Abstract, from a SQLBI article. Link to the original article on SQLBI.com click here https://sql.bi/817168. I just could recommend to read every single article from SQLBI, Alberto Ferrari XOR Marco Russo in full length. Below some key pattern and notes from my side as my personal abstract.
Computing MTD, QTD, YTD in Power BI for the current period
This pattern is to ensure, that MTD, QTD and YTD calculations always reflect values up to the latest transaction, automatically considering the latest available transaction in the data.
Option 1: Calculated columns in Date table
One calculated column will identify dates with transactions, the second calculated column first identifies the MAX(Date[Date]) from all DatesWithTransactions and then returns a string if LastDateWithData is equal to Date[Date].
Column 1
DateWithTransactions =
VAR __LastTransactionDate =
MAXX (
{
MAX ( 'Sales'[Order Date] ),
MAX ( 'Sales'[Delivery Date] )
},
[Value]
)
RETURN
'Date'[Date] <= __LastTransactionDate
-- all dates eqlt LastTransDate = TRUEColumn 2
CurrentDate =
VAR LastDateWithData =
CALCULATE (
MAX ( 'Date'[Date] ),
'Date'[DateWithTransactions] = TRUE,
REMOVEFILTERS ()
)
-- get MAXdate from all dates with transactions
RETURN
IF (
LastDateWithData == 'Date'[Date],
"LastDateWithData"
)
-- identiy single row, where LastDate = DateDateOption 2: Using calculation groups
Create a CalculationGroup and define a CalculationItem „LastDateWithData“.
Last date with data =
VAR LastDateWithData =
CALCULATE (
MAX (
MAX ( Sales[Order Date] ),
MAX ( Sales[Delivery Date] )
),
REMOVEFILTERS ()
)
-- finding last date
RETURN
CALCULATE (
SELECTEDMEASURE (),
'Date'[Date] = LastDateWithData
)
-- filter DateDate = LastDateVariation: Last month with data, but column ‚Date'[Year Month Number] in date table neccessary.
Last month with data =
VAR LastDateWithData =
CALCULATE (
MAX (
MAX ( Sales[Order Date] ),
MAX ( Sales[Delivery Date] )
),
REMOVEFILTERS ()
)
VAR LastMonthWithData =
LOOKUPVALUE (
'Date'[Year Month Number],
'Date'[Date], LastDateWithData
)
RETURN
CALCULATE (
SELECTEDMEASURE (),
'Date'[Year Month Number] = LastMonthWithData,
REMOVEFILTERS ( 'Date' )
)Variation: Last month with data, without column ‚Date'[Year Month Number]
Last month with data (no Date dependencies) =
VAR LastDateWithData =
CALCULATE (
MAX (
MAX ( Sales[Order Date] ),
MAX ( Sales[Delivery Date] )
),
REMOVEFILTERS ()
)
RETURN
CALCULATE (
SELECTEDMEASURE (),
PARALLELPERIOD (
TREATAS (
{ LastDateWithData },
'Date'[Date]
),
0,
MONTH
)
)




Schreibe einen Kommentar