562 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
How to use DAX date-based functions in Analysis Services tabular model Part six of a six-part series of blogs |
---|
There are some wonderful date functions in DAX, with evocative names like TotalYtd, SamePeriodLastYear and ParallelPeriod. This blog shows how to use DAX date functions to summarise data, assuming that you have already created a calendar table.
This blog is part of our online SSAS Tabular tutorial; we also offer lots of other Analysis Services training resources. |
This page shows how you can use DAX measures to extract opening and closing balances from tables of data. You can try out the examples on this page by importing this file into your model, and linking the table created to the Species table and Calendar table:
The species balance table contains balances at particular dates.
Note that for the purposes of this blog, it doesn't matter what the balances are!
Nearly all DAX measures aggregate data:
What you get if you sum the balances from the species balance table.
Here's how the figure 66 for amphibians for January 2013 was reached, for example:
26 + 40 = 66, but sadly your closing monthly bank balance isn't calculated in the same way.
The FIRSTDATE and LASTDATE functions return the first and last days in any table of dates. You could therefore use the LASTDATE function to give (for example) the closing balance for any given period:
Halfway closing balance:=CALCULATE(
SUM(SpeciesBalance[Balance]),
LASTDATE(Calendar[Date])
)
Except that it doesn't often work!
The answers are a bit sparse! The problem is that for most of the balances there isn't a figure for the last day of the calendar month.
For example, for amphibians the only month for which we get a figure is July 2013, because there just happens to be a balance for this month for the final date in the month:
There is a balance for 31st July 2013 for amphibians.
These two functions give - more usefully - the last non-blank value of a function for a range of dates:
The syntax of the LASTNONBLANK function, for example.
Here's how you could use this expression to get the closing balance for each month for each species:
Closing balance:=CALCULATE(
SUM(SpeciesBalance[Balance]),
LASTNONBLANK(
Calendar[Date],
COUNTROWS(RELATEDTABLE('SpeciesBalance'))
)
)
That is, get the balance from the last date in the calendar table for which there are corresponding rows in the SpeciesBalance table.
If you're working a lot with opening and closing balances, investigate the ClosingBalanceYear function, and its equivalents for months and quarters (and for opening balances).
Parts of this blog |
---|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.