0
votes

Problem:

Calculate the total for period of prior year of the period(s) selected in slicer. Table has amount, period, and a date, ie 11/1/2020 for period 11/2020, based on the period.

Attempted:

Slicer periods selected are 10/2020 and 11/2020. Table visual contains the period and amount columns. Data Model Table contains columns, account, amount, period, and period date. Multiple accounts per period will have the same period date, to sort the periods. Example, account 1 through 3 will all have 11/1/2020 as period date for period 11/2020. Calculated Measure created to calculate last year amount is as follows:

Total For Period Last Year = CALCULATE( Sum(‘Table’[Amount]) , Filter( ‘Table’ ,SAMEPERIODLASTYEAR(‘Table’[Period Date]) ) )

Results:

Calculated measure is added to table but only shows the amount for the period selected in slicer and not the period for the same period but of the prior year. So 10/2020 amount would be $2000 and the calculation should show amount for 10/2019 with ie $1500.

Period | Amount | Total For Period Last Year

11/2020 $2000 $2000

10/202. $1500 $1500

Desired Result:

Period | Amount | Total For Period Last Year

11/2020 $2000 $1500

10/2020 $1500 $1000

2

2 Answers

1
votes

To use time intelligence functions a well formed date table should be used. It's possible to create one using various techniques; an easy one can be found here

DAX 101: Creating a simple date table in DAX

That said, the error in your code is that you iterate over the Table part in the current filter context. You might see a better result (but maybe still not correct, because a Date table is missing, using ALL( 'Table' ) instead of just 'Table'. This way you would FILTER over the whole table instead of just the portion in the current period.

Total For Period Last Year =
CALCULATE(
    SUM( 'Table'[Amount] ),
    FILTER( ALL( 'Table' ), SAMEPERIODLASTYEAR( 'Table'[Period Date] ) )
)

I'm not sure, since I never use auto date/time, but this could also work by using the Power BI generated Date table when auto date/time is enabled (and if I correctly remember the synstax on how to use it)

Total For Period Last Year =
CALCULATE(
    SUM( 'Table'[Amount] ),
    FILTER( ALL( 'Table' ), SAMEPERIODLASTYEAR( 'Table'[Period Date].[Date] ) )
)
1
votes

To use Time intelligence like sameperiodlastyear, you must use date table (Mark as Date Table). Read this description:

https://dax.guide/sameperiodlastyear/#

In order to use any time intelligence calculation, you need a well-formed date table. The Date table must satisfy the following requirements:

All dates need to be present for the years required. The Date table must always start on January 1 and end on December 31, including all the days in this range. If the report only references fiscal years, then the date table must include all the dates from the first to the last day of a fiscal year. For example, if the fiscal year 2008 starts on July 1, 2007, then the Date table must include all the days from July 1, 2007 to June 30, 2008. There needs to be a column with a DateTime or Date data type containing unique values. This column is usually called Date. Even though the Date column is often used to define relationships with other tables, this is not required. Still, the Date column must contain unique values and should be referenced by the Mark as Date Table feature. In case the column also contains a time part, no time should be used – for example, the time should always be 12:00 am. The Date table must be marked as a date table in the model, in case the relationship between the Date table and any other table is not based on the Date. Remarks The dates argument can be any of the following:

A reference to a date/time column. Only in this case a context transition applies because the column reference is replaced by CALCULATETABLE ( DISTINCT ( ) ) A table expression that returns a single column of date/time values. A Boolean expression that defines a single-column table of date/time values. The result table includes only dates that exist in the dates column.

Internally SAMEPERIODLASTYEAR corresponds to the following call of DATEADD:

DATEADD ( <Dates>, -1, YEAR )