Calculate prior 6 months. Power BI - powerbi

Sample Data
In my data, I have to calculate the current 6 months of usage and prior 6 months of usage. I already have calculations for the current 6 months. Now I need to do the calculations for 6 months Prior to the current 6 months. Can someone have any advice?
Current 6 Months =
CALCULATE (
[Full Price (modified)],
DATESINPERIOD (
'Dates_Jul20-Jun22'[Date],
MAX ( 'Dates_Jul20-Jun22'[Date] ),
-6,
MONTH
)
)
Same with the 3 months prior to the current 3 months.
Current 3 Months =
CALCULATE (
[Full Price (modified)],
DATESINPERIOD (
'Dates_Jul20-Jun22'[Date],
MAX ( 'Dates_Jul20-Jun22'[Date] ),
-3,
MONTH
)
)

Prior n Months =
VAR _priormonths = 12
VAR _currentmonths = 6
VAR _current =
DATESINPERIOD ( 'Table'[Date], MAX ( 'Table'[Date] ), - _currentmonths, MONTH )
VAR _prior =
DATESINPERIOD ( 'Table'[Date], MAX ( 'Table'[Date] ), - _priormonths, MONTH )
RETURN
CALCULATE ( SUM ( 'Table'[Full Price] ), _prior )
- CALCULATE ( SUM ( 'Table'[Full Price] ), _current )

Related

Sum of values in hourly range

I have the following table
Date
Hour
Value
2022-11-17
15:21:00
1
2022-11-17
22:19:00
2
2022-11-18
00:44:00
2
2022-11-18
04:27:00
3
2022-11-18
13:21:00
2
2022-11-18
23:55:00
1
2022-11-19
09:01:00
4
Now I would like to sum the values based on the day and the specified hour range
It should sum from hour 6:00 of the previous day to hour 6:00 of today.
For example: So the first four rows should be the sum of 2022-11-17 and so on.
Date (6-6)
Sum of values
2022-11-17
8
2022-11-18
3
2022-11-19
4
At the moment I can sum values between hours, but on the same day
Usage =
CALCULATE(
SUM(Table[Value]),
TIME(
HOUR(Table[Hour]),
MINUTE(Table[Hour]),
SECOND(Table[Hour])
) >= TIMEVALUE("02:00:00"),
TIME(
HOUR(Table[Hour]),
MINUTE(Table[Hour]),
SECOND(Table[Hour])
) <= TIMEVALUE("14:00:00")
)
`
You can try this measure:
Usage =
VAR _min_time = SELECTEDVALUE ( 'Table'[Date] ) + TIME ( 6, 0, 0 )
VAR _max_time = SELECTEDVALUE ( 'Table'[Date] ) + 1 + TIME ( 6, 0, 0 )
RETURN
IF (
ISINSCOPE ( 'Table'[Date] ) ,
CALCULATE (
SUM ( 'Table'[Value] ) ,
FILTER (
ALL ( 'Table' ) ,
VAR _time = [Date] + [Hour]
RETURN _min_time < _time && _time <= _max_time
)
)
)
Calculates your desired result:

Cumulative sum by months in Powerbi DAX

I want to show the cumulative sum per month, I have the number per month but need to show the sum up of previous months in each month..
I used the following measure:
RT FAC =
CALCULATE (
CALCULATE(SUM('Injuries'[Total]), 'Injuries'[Classification] = "FAC"),
FILTER(ALLSELECTED('Calendar Table'),
'Calendar Table'[Date]<= 'Calendar Table'[Date]))
But it gave me the total in all the months:
How can I show the running total such as:
What IF you try this with small change?
RT FAC =
VAR MaxDate =
MAX ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
SUM ( 'Injuries'[Total] ),
FILTER (
ALL ( 'Injuries'[Classification] ),
'Injuries'[Classification] = "FAC"
),
FILTER ( ALLSELECTED ( 'Calendar Table' ), 'Calendar Table'[Date] <= MaxDate )
)

DAX/M count for consecutive months for every ID in PowerBI

I need help finding a way to count the number of consecutive months:
For every "ID" (Text Column)
Having the column "Status" either "Missing" or "On hold"
Here is what my table looks like (the last two columns are the output I would like to see):
ID
Year
Month
Date (01-Month-Year)
Status
Consecutive Months "Missing"
Consecutive Months "On Hold"
ID40
2019
6
01/06/2019
Missing
0
-
ID40
2019
7
01/07/2019
Missing
2
-
ID40
2019
8
01/08/2019
Missing
3
-
ID40
2019
11
01/11/2019
Missing
0
-
ID40
2019
12
01/12/2019
Missing
2
-
ID40
2020
9
01/09/2020
Missing
0
-
ID499
2019
1
01/01/2019
On Hold
-
0
ID499
2019
2
01/02/2019
On Hold
-
2
ID499
2019
3
01/03/2019
On Hold
-
3
ID499
2020
9
01/09/2020
On Hold
-
0
ID499
2020
10
01/10/2020
On Hold
-
2
ID499
2020
8
01/08/2020
Missing
0
-
ID499
2020
9
01/09/2020
Missing
2
-
ID499
2020
10
01/10/2020
Missing
3
-
ID499
2020
11
01/11/2020
Missing
4
-
ID499
2020
12
01/12/2020
Missing
5
-
Is there any way to do this besides with merged nested queries in "M"? Unfortunately I have already tried this, but PowerBI has trouble processing the data.
Thanks everyone in advance!
A possible solution is to build a table of the Years and Months since the first Year in the table and remove from it the Year Months that appear in the table. Than compute the difference in months between the current Year Month and the last non existent Year Month.
This is a possible calculated column that uses the EXCEPT function to build the table with the missing Year Months. To ease the computation of the difference in months across the years, it also prepares a YearMonthNumber value with the progressive month number from the first year month
Since in the example table there are separate columns for the Status, I added an IF to split the columns accordingly
I also added a check to return 0 instead of 1, to match the example data
Consecutive Months Missing =
IF (
T[Status] = "Missing",
VAR FirstYear =
MIN ( T[Year] )
VAR CurrentYear = T[Year]
VAR CurrentMonth = T[Month]
VAR CurrentYearMonthNumber = ( CurrentYear - FirstYear ) * 12 + CurrentMonth
VAR YearMonths =
GENERATE (
SELECTCOLUMNS ( GENERATESERIES ( FirstYear, CurrentYear ), "Year", [Value] ),
SELECTCOLUMNS (
GENERATESERIES ( 1, 12, 1 ),
"Month", [Value],
"YearMonthNumber",
( [Year] - FirstYear ) * 12 + [Value]
)
)
VAR CurrentIDAndStatusYearMonths =
CALCULATETABLE (
ADDCOLUMNS (
SUMMARIZE ( T, T[Year], T[Month] ),
"YearMonthNumber",
( T[Year] - FirstYear ) * 12 + T[Month]
),
ALLEXCEPT ( T, T[ID], T[Status] )
)
VAR MissingYearMonths =
EXCEPT ( YearMonths, CurrentIDAndStatusYearMonths )
VAR FirstMissingYearMonthNumber =
MAXX (
FILTER ( MissingYearMonths, [YearMonthNumber] < CurrentYearMonthNumber ),
[YearMonthNumber]
)
VAR Result = CurrentYearMonthNumber - FirstMissingYearMonthNumber
RETURN
IF ( Result = 1, 0, Result )
)
and
Consecutive Months On Hold =
IF (
T[Status] = "On Hold",
VAR FirstYear =
MIN ( T[Year] )
VAR CurrentYear = T[Year]
VAR CurrentMonth = T[Month]
VAR CurrentYearMonthNumber = ( CurrentYear - FirstYear ) * 12 + CurrentMonth
VAR YearMonths =
GENERATE (
SELECTCOLUMNS ( GENERATESERIES ( FirstYear, CurrentYear ), "Year", [Value] ),
SELECTCOLUMNS (
GENERATESERIES ( 1, 12, 1 ),
"Month", [Value],
"YearMonthNumber",
( [Year] - FirstYear ) * 12 + [Value]
)
)
VAR CurrentIDAndStatusYearMonths =
CALCULATETABLE (
ADDCOLUMNS (
SUMMARIZE ( T, T[Year], T[Month] ),
"YearMonthNumber",
( T[Year] - FirstYear ) * 12 + T[Month]
),
ALLEXCEPT ( T, T[ID], T[Status] )
)
VAR MissingYearMonths =
EXCEPT ( YearMonths, CurrentIDAndStatusYearMonths )
VAR FirstMissingYearMonthNumber =
MAXX (
FILTER ( MissingYearMonths, [YearMonthNumber] < CurrentYearMonthNumber ),
[YearMonthNumber]
)
VAR Result = CurrentYearMonthNumber - FirstMissingYearMonthNumber
RETURN
IF ( Result = 1, 0, Result )
)
this is the resulting table put in a table visual

Trailing Twelve Months SUM not calculating in powerBI correctly

Trying to add a measure in PowerBI that calculates the rolling 12-month sum of sales and the measure works fine up until the most recent 12 months worth of data. Not sure what's causing this error. Below is the data and code in PowerBI I'm using.
TTM MRR =
CALCULATE (
SUM ( MRR[MONTHLY_REV] ),
FILTER (
ALL ( MRR[CLOSE_MONTH] ),
AND (
MRR[CLOSE_MONTH] <= MAX ( MRR[CLOSE_MONTH] ),
DATEADD ( MRR[CLOSE_MONTH], 1, YEAR ) > MAX ( MRR[CLOSE_MONTH] )
)
)
)
Data:
[Excel Data]
It might be easier with DATESINPERIOD.
TTM MRR =
VAR PeriodEnd = MAX ( MRR[CLOSE_MONTH] )
RETURN
CALCULATE (
SUM ( MRR[MONTHLY_REV] ),
DATESINPERIOD ( MRR[CLOSE_MONTH], PeriodEnd, -12, MONTH )
)

DAX time intelligence functions with nested filters

I have a data structure like this
DateRoll Dataset Date Value Customer
Month Online 1/1/2018 10 Cust1
Month Online 2/1/2018 11 Cust1
Month Online 3/1/2018 12 Cust1
Month Online 4/1/2018 22 Cust1
Quarter Online 1/1/2018 33 Cust1
Quarter Online 4/1/2018 22 Cust1
I have to calculate previous quarter value, I tried different ways but it's not working
1 - Not returning any value.
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
DATEADD ( 'Data_Rollup_KPI_DNR'[Date].[Date], -1, QUARTER ),
FILTER ( Data_Rollup_KPI_DNR, Data_Rollup_KPI_DNR[DateRoll] = "Quarter")
)
2--Nested - Returning overall total
CALCULATE (
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
DATEADD ( 'Data_Rollup_KPI_DNR'[Date].[Date], -1, QUARTER )
),
FILTER ( Data_Rollup_KPI_DNR, Data_Rollup_KPI_DNR[DateRoll] = "Quarter" )
)
3--Nested --Returning overall total
CALCULATE (
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
FILTER ( Data_Rollup_KPI_DNR, Data_Rollup_KPI_DNR[DateRoll] = "Quarter" )
),
DATEADD ( 'Data_Rollup_KPI_DNR'[Date].[Date], -1, MONTH )
)
Tried PREVIOUSQUARTER function too, but its not returning any value.
To take advantage of built in DAX time intelligence functions you will need to to have a contiguous set of dates. I would recommend using a date table. The following code can be used to create a date/calendar table in your model:
Celndar =
Var MinDate = MIN(Data_Rollup_KPI_DNR[Date])
Var MaxDate = MAX(Data_Rollup_KPI_DNR[Date])
Var BaseCalendar = CALENDAR(MinDate, MaxDate)
RETURN
GENERATE (
BaseCalendar,
VAR BaseDate = [Date]
VAR YearDate =
YEAR ( BaseDate )
VAR MonthNumber =
MONTH ( BaseDate )
VAR YrMonth =
100 * YEAR ( BaseDate )
+ MONTH ( BaseDate )
VAR Qtr =
CONCATENATE ( "Q", CEILING ( MONTH ( BaseDate ) / 3, 1 ) )
RETURN
ROW (
"Day", BaseDate,
"Year", YearDate,
"Month Number", MonthNumber,
"Month", FORMAT ( BaseDate, "mmmm" ),
"Year Month", FORMAT ( BaseDate, "mmm yy" ),
"YrMonth", YrMonth,
"Qtr", Qtr
)
)
Once this table exists, mark it as a 'date' table and create a relationship with
Data_Rollup_KPI_DNR[Date]
Then, you can write the following measure to obtain the results you are searching for:
PQSum =
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
PREVIOUSQUARTER ( 'Calendar'[Date] )
)
Hope that helps!
*Edited
You can also create a ranking column to index in a measure:
Rank =
RANKX (
FILTER (
'Data_Rollup_KPI_DNR',
'Data_Rollup_KPI_DNR'[DateRoll] = EARLIER ( 'Data_Rollup_KPI_DNR'[DateRoll] )
),
'Data_Rollup_KPI_DNR'[Date].[Date],
,
ASC
)
Then you can reference a previous quarter using something like the following:
PQSum2 =
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
FILTER (
'Data_Rollup_KPI_DNR',
'Data_Rollup_KPI_DNR'[Rank]
= MAX ( 'Data_Rollup_KPI_DNR'[Rank] ) - 1
),
'Data_Rollup_KPI_DNR'[DateRoll] = "Quarter"
)
but this is hard coded and just plain nasty!
Echoing #steliok that a date dimension is the proper way to handle this; there are plenty of date table templates out there, and a date dimension will work with your data model. If you really really can't add to your data structure for some reason, this should work:
BaseValue = SUM ( 'Data_Rollup_KPI_DNR'[Value] )
PriorQuarter =
VAR CurrentDate = MAX ( 'Data_Rollup_KPI_DNR'[Date] )
VAR CurrentYear = YEAR ( CurrentDate )
VAR CurrentMonth = MONTH ( CurrentDate )
VAR FirstMonthOfCurrentQuarter =
SWITCH (
TRUE (),
CurrentMonth IN {1,2,3}, 1,
CurrentMonth IN {4,5,6}, 4,
CurrentMonth IN {7,8,9}, 7,
CurrentMonth IN {10,11,12}, 10
)
// DATE() does the right thing with negative month args
VAR PriorQuarterDate = DATE ( CurrentYear, FirstMonthOfCurrentQuarter - 3, 1 )
RETURN
CALCULATE (
[BaseValue],
ALL ( 'Data_Rollup_KPI_DNR'[DateRoll], 'Data_Rollup_KPI_DNR'[Date] ),
'Data_Rollup_KPI_DNR'[Date] = PriorQuarterDate,
'Data_Rollup_KPI_DNR'[DateRoll] = "Quarter"
)
This relies on DATE being clever, which it is. DATE ( 2019, -2, 1 ) = DATE ( 2018, 10, 1 ).
Ultimately, my question is why can't you just source the un-rolled up data from the same place that the ETL process is sourcing it?
Date functions are working well when you are using # Day level.
Following link would be helpful to resolve your issue,
https://community.powerbi.com/t5/Desktop/Lead-and-Lag-in-DAX/td-p/649162