0% found this document useful (0 votes)
51 views8 pages

Time Intelligence Functions

Lecture Notes on Time Intelligence Function

Uploaded by

Abhyudya Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views8 pages

Time Intelligence Functions

Lecture Notes on Time Intelligence Function

Uploaded by

Abhyudya Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

09-04-2023

Time intelligence Introducing time intelligence Introducing time intelligence


• In this chapter, you learn how to implement common date-related • Slicing data by year and month, it is preferable to use the columns of • One should always create at least one date table whenever there are
calculations such as year-to-date, year-over-year, and other a table specifically designed to slice dates. one or more date columns in the data.
calculations over time including nonadditive and semi-additive • Extracting the date parts from a single column of type Date or • Power BI offer embedded features to automatically create tables or
measures. DateTime in calculated columns is a less desirable approach. columns to manage dates in the model.
• You learn both how to use specific time intelligence functions and • If a model contains multiple dates, like the order date and the • These features come with several restrictions, it is usually better to
how to rely on custom DAX code for nonstandard calendars and delivery date, then one can either create multiple relationships with a use your own date table.
week-based calculations. single date table or duplicate the date table. The resulting models are
different, and so are the calculations.

07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 194 07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 195 08-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 196

Time intelligence calculations Date/Time feature drawbacks Building a date table


• When the setting is enabled—it • The Auto Date/Time feature comes with two major drawbacks: • There are two different aspects to consider: a technical aspect and a data
is by default—Power BI modeling aspect.
automatically creates a date • Power BI Desktop generates one table per date column.
• This creates an unnecessarily high number of date tables in the model, • From a technical point of view, the date table must follow these guidelines:
table for each Date or DateTime
column in the model. unrelated to one another. • The date table contains all dates included in the period to analyze. For example, if
the minimum and maximum dates contained in Sales are July 3, 2016, and July 27,
• This makes it possible to slice • Building a simple report presenting the amount ordered and the amount sold
2019, respectively, the range of dates of the table is between January 1, 2016, and
in the same matrix proves to be a real challenge.
each date by year, quarter, December 31, 2019.
month, and day. • The tables are hidden and cannot be modified by the developer. • In other words, the date table needs to contain all the days for all the years
• Consequently, if one needs to add a column for the weekday, they cannot. containing sales data.
• These automatically created
• There can be no gaps in the sequence of dates.
tables are hidden to the user
and cannot be modified. • All dates need to be present, regardless of whether there are transactions or not on
each date.

07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 197 07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 198 07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 199

Building a date table Building a date table Using CALENDAR and CALENDARAUTO
• The date table contains one column of DateTime type, with unique values. • Using a table with only the relevant years is a best practice. • If you do not have a date table in your data source, you can create the date table
• The Date data type is a better choice because it guarantees that the time part is • A good date table should include a rich set of columns that greatly by using either CALENDAR or CALENDARAUTO.
empty. improve the user experience. • These functions return a table of one column, of DateTime data type.
• If the DateTime column also contains a time part, then all the times of the day need • CALENDAR requires you to provide the upper and lower boundaries of the set of
to be identical throughout the table. • If necessary, additional columns can be created as calculated columns dates.
or by changing the source query.
• It is not necessary that the relationship between Sales and the date table • CALENDARAUTO scans all the date columns across the entire data model, finds
be based on the Date-Time column. • You can create the date table by using a DAX calculated table the minimum and maximum years referenced, and finally generates the set of
• One can use an integer to relate the two tables, yet the DateTime column needs to
(CALENDAR and CALENDARAUTO functions). dates between these years.
be present. • The term “Date” is a reserved keyword in DAX; it corresponds to the • A simple calendar table containing all the dates in the Sales table can be created
• The table should be marked as a Date table. DATE function. using the following code Only year
• Though this is not a strictly mandatory step, it greatly helps in writing correct code. • Therefore, you should embed the Date name in quotes when referring to the extracted
table name.
07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 200 07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 201 07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 202
09-04-2023

Using CALENDAR and CALENDARAUTO Using CALENDAR and CALENDARAUTO Using CALENDAR and CALENDARAUTO
• A similar result can be obtained by using the simpler CALENDARAUTO: • One can restrict the result of CALENDARAUTO to only the desired set • Once the developer has obtained the correct list of dates, they still must create additional columns using
DAX expressions. F08 04.pbix
• Date = CALENDARAUTO ( ) of dates, as follows
• CALENDARAUTO scans all the date columns, except for calculated columns.
• For example, if one uses CALENDARAUTO to create a Date table in a model that The resulting table only
contains sales between 2007 and 2011 and has an AvailableForSaleDate column in contains the useful dates.
the Product table starting in 2004, the result is the set of all the days between Finding the first and last day of
January 1, 2004, and December 31, 2011. the year is not that important
• However, if the data model contains other date columns, they affect the date range because CALENDARAUTO
F0804.pbix
considered by CALENDARAUTO. handles this internally.

• For example, if among the many dates a model also contains the customers’
birthdates.

07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 203 07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 204 07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 205

Handling multiple relationships to the Date Handling multiple relationships to the Date
Working with multiple dates
table table
• When there are multiple date columns in the model, you should • One can create multiple relationships between two tables. Nevertheless, only • The first measure, Ordered Amount, uses the active relationship
consider two design options: creating multiple relationships to the one relationship can be active. between Sales and Date, based on Sales[Order Date].
same date table or creating multiple date tables. • The other relationships need to be kept inactive. Inactive relationships can be
• The second measure, Delivered Amount, executes the same DAX
activated in CALCULATE through the USERELATIONSHIP
• Consider a Sales table with the following three dates for every sales expression using the relationship based on Sales[Delivery Date].
transaction: • F0805.pbix. There are two different relationships between Sales and Date, but
only one can be active. In the example, the active relationship is the one between • USERELATIONSHIP changes the active relationship between Sales and
• Order Date: the date when an order was received. Sales[Order Date] and Date[Date]. Date in the filter context defined by CALCULATE. F0806.pbix.
• Due Date: the date when the order is expected to be delivered.
• Create two measures for the sales amount based on a different relationship to • Using multiple relationships with a single date table increases the
• Delivery Date: the actual delivery date. the Date table number of measures in the data model.

07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 206 07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 207 07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 208

Handling multiple date tables Handling multiple date tables Handling multiple date tables
• An alternative approach is to create different date tables—one for each • To create a Delivery Date table and an Order Date table, you add the
date in the model—so that every measure aggregates data according to the same table twice in the data model.
date selected in the report.
• You must physically duplicate the Date table.
• This might seem like a better solution because it lowers the number of • Therefore, it is a best practice to create different views in the data source, one
measures, and it allows for the selecting of sales that intersect between for each role dimension, so that each date table has different column names
two months, but it produces a model that is harder to use. and different content.
• This approach is also known as the role-playing dimension approach. The • For example, instead of having the same Year column in all the date tables, it
date table is a dimension that you duplicate once for each relationship— is better if you use Order Year and Delivery Year.
that is, once for each of its roles. • Furthermore, it is also a good practice to change the content of columns; for
• These two options (using inactive relationships and duplicating the date instance, by placing a prefix for the year depending on the role of the date.
table) are complementary to each other. • As an example, one might use the CY prefix for the content of the Order Year
column and the DY prefix for the content of the Delivery Year column.
F08 07.pbix, See the codes.
07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 209 07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 210 07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 211
09-04-2023

Understanding basic time intelligence Understanding basic time intelligence


Handling multiple date tables
calculations calculations
• It would be wrong to choose multiple date tables just to reduce the • DAX provides several time intelligence functions. • General explanation of how time intelligence calculations work:
number of measures because this makes it impossible to create a report • Sales Amount := SUMX ( Sales, Sales[Net Price] * Sales[Quantity] )
with the same measures grouped by two dates. • Not easy to start using those functions without a good understanding
• For example, consider a single line chart showing Sales Amount by Order of their inner details. • To compute a YTD when the filter context is filtering February 2007, they would
Date and Delivery Date. need to change the filter context to include January and February 2007, before
• First we will learn how to author any time intelligence calculation by performing the iteration over Sales.
• One needs a single Date table in the date axis of the chart, and this would using standard DAX functions such as CALCULATE, CALCULATETABLE,
be extremely complex to achieve with the multiple date tables pattern. • Use a filter argument in a CALCULATE function which returns the year-to-date up
FILTER, and VALUES. to February 2007:
• The main scenario where multiple date tables are useful is to intersect the • Then we will learn how the time intelligence functions in DAX help
same measure by different dates in the same visualization.
you shorten your code.
• In most other scenarios, a single date table with multiple relationships is a
better choice. F0809.pbix

07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 212 07-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 213 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 214

Understanding basic time intelligence Understanding basic time intelligence


Introducing basic time intelligence functions
calculations calculations
• The FILTER function used as a filter argument of CALCULATE returns a • Simple calculations like year-to-date, quarter-to-date, month-to-date,
set of dates that replaces the selection of the Date table. or the comparison of sales in the current year versus the previous
• In other words, even though the original filter context coming from year can be authored with simpler code as they all rely on basic time
the rows of the matrix filters an individual month, the measure intelligence functions.
computes the value on a different set of dates. • All time intelligence functions in DAX apply a filter condition on the
date column of a Date table.
• http://www.daxpatterns.com/time-patterns/.

09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 215 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 216 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 217

Using year-to-date, quarter-to-date, and Using year-to-date, quarter-to-date, and Using year-to-date, quarter-to-date, and
month-to-date month-to-date month-to-date
• Month-to-date is meaningful only when you are looking at data at the day level, • Similar to DATESYTD, there are another two functions that return the • Previous approach requires the use of CALCULATE.
whereas year-to-date and quarter-to-date calculations are often used to look at month-to-date (DATESMTD) and quarter-to-date (DATESQTD) sets. • DAX also offers a set of functions to simplify the syntax of to-date
data at the month level. calculations: TOTALYTD, TOTALQTD, and TOTALMTD. Do not require explicit
use of CALCULATE.
• DATESYTD is a function that returns a table with all the dates from the beginning
of the year until the last date included in the current filter context. • YTD Sales := TOTALYTD (Sales Amount], 'Date'[Date])
• TOTALYTD requires the expression to aggregate as its first parameter and
• This table is used as a filter argument in CALCULATE to set the new filter for the the date column as its second parameter.
Sales Amount calculation. F0813.pbix
• QTD Sales := TOTALQTD ( [Sales Amount], 'Date'[Date] ) or
• QTD Sales := CALCULATE ( [Sales Amount], DATESQTD ( 'Date'[Date] ) )
• MTD Sales := TOTALMTD ( [Sales Amount], 'Date'[Date] ) or
• MTD Sales := CALCULATE ( [Sales Amount], DATESMTD ( 'Date'[Date] ) )

09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 218 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 219 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 220
09-04-2023

Using year-to-date, quarter-to-date, and


Computing time periods from prior periods Computing time periods from prior periods
month-to-date
• Calculating a year-to-date measure over a fiscal year that does not end on • Useful for making comparisons of trends during a time period this year to • In a similar way, DATEADD can compute the value from a previous
December 31 requires an optional third parameter that specifies the end the same time period last year.
day of the fiscal year. quarter (PQ), month (PM), or day (PD):
• SAMEPERIODLASTYEAR comes in handy: • PQ Sales := CALCULATE ( [Sales Amount], DATEADD ( 'Date'[Date], -1,
• For example, both the following measures calculate the fiscal year-to-date
for Sales: • PY Sales := CALCULATE ( [Sales Amount], SAMEPERIODLASTYEAR ( 'Date'[Date] ) ) QUARTER ) )
• Fiscal YTD Sales := TOTALYTD ( [Sales Amount], 'Date'[Date], "06-30" ) or • SAMEPERIODLASTYEAR is a specialized version of the more generic • PM Sales := CALCULATE ( [Sales Amount], DATEADD ( 'Date'[Date], -1, MONTH
• Fiscal YTD Sales := CALCULATE ( [Sales Amount], DATESYTD ( 'Date'Date], "06-30" )) DATEADD function, which accepts the number and type of period to shift. ))
• You can also consider using a string with the format YYYY-MM-DD to avoid The types of periods supported are YEAR, QUARTER, MONTH, and DAY. • PD Sales := CALCULATE ( [Sales Amount], DATEADD ( 'Date'[Date], -1, DAY ) )
any ambiguity caused by culture settings; in that case, the year does not • For example, you can define the same PY Sales measure using this
matter for the purpose of determining the last day of the year to use for • F08 14.pbix
year-to-date calculation: equivalent expression, which uses DATEADD to shift the current filter
• Fiscal YTD Sales := CALCULATE ( [Sales Amount], DATESYTD ( 'Date'[Date], "2018-06- context one year back in time:
30" ) ) • PY Sales := CALCULATE( [Sales Amount], DATEADD ( 'Date'[Date], -1, YEAR ) )

09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 221 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 222 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 223

Computing time periods from prior periods Computing time periods from prior periods Computing time periods from prior periods
• Another useful function is PARALLELPERIOD, which is similar to DATEADD,
but returns the full period specified in the third parameter instead of the
partial period returned by DATEADD.
• Thus, although a single month is selected in the current filter context, the
following measure using PARALLEPERIOD calculates the amount of sales for
the whole previous year:
F08 14.pbix • PY Total Sales := CALCULATE ( [Sales Amount], PARALLELPERIOD ( 'Date'[Date], -1, F08 15.pbix
YEAR ) )
• In a similar way, using different parameters, one can obtain different
periods:
• PQ Total Sales := CALCULATE ( [Sales Amount], PARALLELPERIOD ( 'Date'[Date], -1,
QUARTER ) )

09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 224 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 225 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 226

Computing time periods from prior periods Computing time periods from prior periods Computing time periods from prior periods
• There are functions similar but not identical to PARALLELPERIOD, which are • For example, the following code returns March, April, and May 2008 in
PREVIOUSYEAR, PREVIOUSQUARTER, PREVIOUSMONTH, PREVIOUSDAY, case the second quarter of 2008 (April, May, and June) is selected:
NEXTYEAR, NEXTQUARTER, NEXTMONTH, and NEXTDAY. • PM Total Sales :=CALCULATE ( [Sales Amount], PARALLELPERIOD ( 'Date'[Date], -1,
MONTH ) )
• These functions behave like PARALLELPERIOD when the selection has a
single element selected corresponding to the function name—year, • Conversely, the following code only returns March 2008 in case the second
quarter, month, and day. quarter of 2008 (April, May, and June) is selected.
• Last PM Sales :=CALCULATE ( [Sales Amount], PREVIOUSMONTH( 'Date'[Date] ) ) F08 16.pbix
• If multiple periods are selected, then PARALLELPERIOD returns a shifted
result of all of them. • F08 16.pbix. The Last PM Sales measure returns the value of December
• On the other hand, the specific functions (year, quarter, month, and day, 2007 for both 2008 and Q1 2008, whereas PM Total Sales always returns
respectively) return a single element that is contiguous to the selected the value for the number of months of the selection—three for a quarter
period regardless of length. and twelve for a year. This occurs even though the initial selection is shifted
back one month.

09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 227 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 228 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 229
09-04-2023

Mixing time intelligence functions Mixing time intelligence functions Mixing time intelligence functions
• One useful feature of time intelligence functions is the capability of composing • Time intelligence functions accept a table as their first parameter, and they
more complex formulas by using time intelligence functions together. act as time shifters.
• The first parameter of most time intelligence functions is the date column in the • These functions take the content of the table, and they shift it back and
forth over time by any number of years, quarters, months, or days. Note that exchanging
date table. However, this is just syntax sugar for the complete syntax. the order of the
• Because time intelligence functions accept a table, any table expression function calls does not
• In fact, the full syntax of time intelligence functions requires a table as its first can be used in place of the table—including another time intelligence change the result.
parameter. function.
• This makes it possible to combine multiple time intelligence functions, by
cascading their results one into the other.
• For example, the following code compares the year-to-date with the
corresponding value in the previous year. It does so by combining
SAMEPERIODLASTYEAR and DATESYTD.

09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 230 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 231 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 232

Mixing time intelligence functions Mixing time intelligence functions Computing a difference over previous periods
• It is also possible to use CALCULATE to move the current fi lter context • A common operation is calculating the difference between a measure
to a different time period and then invoke a function that, in turn, and its value in the prior year.
analyzes the filter context and moves it to a different time period. • You can express that difference as an absolute value or as a
• The following two definitions of PY YTD Sales are equivalent to the percentage.
previous two. • Recall PY Sales := CALCULATE ( [Sales Amount], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
F08 17.pbix • For Sales Amount, the absolute difference over the previous year
(year-over-year or YOY) is a simple subtraction.
F08 17.pbix • However, you need to add a failsafe if you want to only show the
difference when both values are available.

09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 233 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 234 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 235

Computing a difference over previous periods Computing a difference over previous periods Computing a difference over previous periods
• The equivalent calculation for comparing the year-to-date measure with a • Often, the year-over-year difference is better expressed as a
corresponding value in the prior year is a simple subtraction of two measures, percentage in a report.
YTD Sales and PY YTD Sales.
• YOY Sales% := DIVIDE ( [YOY Sales], [PY Sales] )
• The DIVIDE function avoids a divide-by-zero error if there is no corresponding
data in the prior year
• A similar calculation displays the percentage difference of a year-over-
year comparison for the year-to-date aggregation.
• YOY YTD Sales% := DIVIDE ( [YOY YTD Sales], [PY YTD Sales] )
• Refer F08 18.pbix

09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 236 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 237 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 238
09-04-2023

Computing a moving annual total Computing a moving annual total Computing a moving annual total
• Another common calculation that eliminates seasonal changes in • Dividing this value by the
sales is the moving annual total (MAT), which considers the sales number of months included in
aggregation over the past 12 months. the period averages it over the
• For example, summing the range of dates from April 2007 to March time frame.
2008 calculates the value of MAT Sales for March 2008. • This gives you a moving annual
• The easiest approach is to use the DATESINPERIOD function. average (MAA): (F08 19.pbix)
• DATESINPERIOD returns all the dates included within a period that
can be a number of years, quarters, months, or days.

09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 239 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 240 09-04-2023 DataF08 19.pbix
Vizualization using Power BI. (c) Dr. Achint Nigam 241

Computing a moving annual total Understanding semi-additive calculations Understanding semi-additive calculations
• In general, all time intelligence functions return sets of existing dates. • A semi-additive measure uses one kind of aggregation (typically a sum) • The sample data shows that the
when sliced by certain columns and a different kind of aggregation (usually balance of Katie Jordan at the end
• If a date does not belong to the Date table, then these functions of January was 1,687.00, whereas
the last date) when sliced by other columns. at the end of February the balance
return an empty table that corresponds to a blank scalar value. was 2,812.00.
• A great example is the balance of a bank account.
• In some scenarios this behavior might produce unexpected results, • When we look at January and
• The balance of all the customers is the sum of each individual balance. February together, her balance is
not the sum of the two values.
• However, the balance over a full year is not the sum of monthly balances. • Instead, it is the last balance
• Instead it is the balance on the last date of the year. available.
• On the other hand, the overall
• Slicing the balance by customer results in a regular calculation, whereas balance of all customers in January
slicing by date means the calculation follows a different path. F08 21.pbix is the sum of the three customers
together. F08 21.pbix

09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 242 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 243 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 244

Understanding semi-additive calculations Using LASTDATE and LASTNONBLANK Using LASTDATE and LASTNONBLANK
• LASTDATE returns a table only containing one row, which represents • LASTDATE is simple to use; unfortunately, LASTDATE is not the correct
the last date visible in the current filter context. solution for many semiadditive calculations.
• In fact, LASTDATE scans the date table always returning the last date in the
• When used as a filter argument of CALCULATE, LASTDATE overrides date table.
the filter context on the date table so that only the last day of the
• If the data is not available on the specific date returned by LASTDATE, the
selected period remains visible. result of the calculation is blank. F08 24.pbix
• The following code computes the last balance by using LASTDATE to • The total of Q3 and the grand total are not visible. Because the total of Q3 is empty,
the report does not even show Q3, resulting in a confusing result.
overwrite the filter context on Date:
• If, instead of using the month to slice data at the lowest level, we use the
date, then the problem of LASTDATE becomes even more evident. F08
25.pbix.
• The Q3 row now is visible, even though its result is still blank.

09-04-2023 Wrong Data Vizualization using Power BI. (c) Dr. Achint Nigam Correct 245 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 246 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 247
09-04-2023

Using LASTDATE and LASTNONBLANK Using LASTDATE and LASTNONBLANK Using LASTDATE and LASTNONBLANK
• If there are values on dates prior to the last day of the Date table, and that last • When used at the month level, LASTNONBLANK iterates over each
day has no data available, then a better solution is to use the LASTNONBLANK date in the month, and for each date it checks whether the related
function. table with the balances is empty.
• LASTNONBLANK is an iterator that scans a table and returns the last value of the
table for which the second parameter does not evaluate to BLANK. • The innermost RELATEDTABLE function is executed in the row context
of the LASTNONBLANK iterator, so that RELATEDTABLE only returns
• In our example, we use LASTNONBLANK to scan the Date table searching for the
last date for which there are rows in the Balances table. the balances of the given date.
• If there is no data, then RELATEDTABLE returns an empty table and
Explanation on the next slide
COUNTROWS returns a blank.
F08 24.pbix F08 25.pbix • At the end of the iteration, LASTNONBLANK returns the last date that
computed a nonblank result.
09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 248 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 249 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 250

Using LASTDATE and LASTNONBLANK Using LASTDATE and LASTNONBLANK Using LASTDATE and LASTNONBLANK
• If all customer balances are gathered on the same date, then LASTNONBLANK • The reason is when the filter context filters Katie Jordan, the last date • When using a different date for each customer, the grand total cannot
solves the problem.
• In our example, we have different dates for different customers within the same
with some values is July 15. be computed by simply using the filter context at the grand total.
month and this creates another issue. • When the filter context filters Maurizio Macagno, the last date • The formula needs to compute the subtotal of each customer and
• With semi-additive calculations the devil is in the details. F08 26.pbix. becomes July 18. then aggregate the results.
• The result for each individual customer looks correct. Indeed, the last known
balance for Katie • Nevertheless, when the filter context no longer filters the customer • This is one of the scenarios where iterators are a simple and effective
• Jordan is 2,531.00, which the formula correctly reports as her total. The same name, then the last date is Maurizio Macagno’s, which is July 18. solution.
behavior produces correct results for Luis Bonifaz and Maurizio Macagno.
Nevertheless, the grand total seems wrong. • Neither Katie Jordan nor Luis Bonifaz have any data on July 18. • Indeed, the following measure uses an outer SUMX to produce the
• Indeed, the grand total is 1,950.00, which is the value of Maurizio Macagno only. • Therefore, for the month of July the formula only reports the value of total by summing the individual values of each customer:
It is confusing for a report to show a total composed in theory of three values
(2,531.00, 2,205.00, 1,950.00) that only sums up the last value. Maurizio Macagno.

09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 251 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 252 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 253

Using LASTDATE and LASTNONBLANK Using LASTDATE and LASTNONBLANK Working with opening and closing balances
• The result of this latter measure • DAX offers many functions that simplify calculations retrieving the value of
a measure at the opening or closing date of a time period.
computes for each customer the
• Although useful, these additional functions suffer from the same
value on their own last date. There are two similar functions limitations as LASTDATE. That is, they work well if and only if the dataset
• It then aggregates the grand available to obtain the first date contains values for
total by summing individual instead of the last date within a time • all the dates.
values. period. These functions are • These functions are STARTOFYEAR, STARTOFQUARTER, STARTOFMONTH,
FIRSTDATE and FIRSTNONBLANK. and the corresponding closing functions: ENDOFYEAR, ENDOFQUARTER,
• F08 28.pbix. (next slide). ENDOFMONTH.
• Intuitively, STARTOFYEAR always returns January 1 of the currently selected
year in the filter context.
• In a similar way STARTOFQUARTER and STARTOFMONTH return the
beginning of the quarter or of the month, respectively.
09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 254 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 255 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 256
09-04-2023

Working with opening and closing balances Working with opening and closing balances Working with opening and closing balances
• F 08 29.pbix see data. • However, this nice result is not due to • Using LASTDATE means you can expect empty values whenever there
the DAX code working well.
• The demo fi le contains the prices of the Microsoft stock between is no value on the exact last day of the month.
• The chart looks correct because we
2013 and 2018. used the date level in the x axis, and • That day might be either a weekend or a holiday.
the client tool—Power BI in this
• The value is well known at the day level. But what should a report example—works hard to ignore all the • The correct version of Last Value is:
show at an aggregated level—for example, at the quarter level? empty values in our dataset.
• The following formula computes the last value of the Microsoft stock, • This results in a continuous line.
considering an average of the prices in case there are multiple rows • But using the same measure in a
for the same day: matrix sliced by year and month
would make the gaps in the
calculation become much more
evident. F 08 30.pbix
09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 257 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 258 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 259

Working with opening and closing balances Working with opening and closing balances
• STARTOFQUARTER returns the • To compute the start or the end of a time period, only taking into
date when the current quarter account dates with data available, the functions to use are
started, regardless the presence FIRSTNONBLANK and LASTNONBLANK mixed with other time
of data on that specific date. intelligence functions like, for example, DATESINPERIOD.
• For example, January 1, which is
the start of the first quarter, is
also New Year’s Day.
F 08 32.pbix
• Consequently, there is never a
price for a stock on that date.
• F 08 31.pbix (wrong)
09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 260 09-04-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 261

You might also like