YTD Average of Measure - powerbi

Can anyone help me in creating a YTD Average % Calculation?
I have created 'A','B' by using the following DAX
A = DIVIDE([Current Month W/Allowance Over 90 $],[Aging],0)
B = DIVIDE([UnderBill],[OverBill],0)
Now I need to create a YTD Average % calculation based on the above two calculations.
This is what I am looking for
YTD A = Average of 'A' ( This average should be YTD)
YTD B = Average of 'B' ( This average should be YTD)
enter image description here
So if we look at YTD A for 08/01/2019, in excel I did the Average =AVERAGE(C2:C9) and the result is 65% and for next month it should be =AVERAGE(C2:C10) and the result is 61%

Assuming there is a Calendar table which has Calendar[Year] and Calendar[YearMonth] columns, here is a possible solution.
YTD A =
-- Calculated table which has start and end dates of each YTD months
VAR YearMonthsYTD =
ADDCOLUMNS(
SUMMARIZE(
FILTER(
ALL( 'Calendar' ),
'Calendar'[Year] = MAX( 'Calendar'[Year] )
&& 'Calendar'[Date] <= MAX( 'Calendar'[Date] )
),
'Calendar'[YearMonth]
),
"#StartDate", CALCULATE( MIN( 'Calendar'[Date] ) ),
"#EndDate", CALCULATE( MAX( [Date] ) )
)
-- Calculate the value for each month, and get the average of them
RETURN
AVERAGEX(
YearMonthsYTD,
CALCULATE(
[A],
FILTER(
ALL( 'Calendar' ),
'Calendar'[Date] >= [#StartDate] && 'Calendar'[Date] <= [#EndDate]
)
)
)
BTW, although I'm ignorant in accounting, I'm doubting if your calculation logic is correct, because average of percentages will not be an appropriate measure in general.
In the presented scenario, wouldn't this definition be more appropriate?
YTD A = DIVIDE( <YTD value of numerator>, <YTD value of denominator> )
This is not only to make it correct, but also to make it much easier.
YTD A = CALCULATE( [A], DATESYTD( 'Calendar'[Date] ) )
Forgive and forget if this is not a relevant comment for the specific case of OP.

Related

Totals not showing correctly in Power BI Table summing a measure

I have a table summing Meter Hrs. The total is incorrectly showing zero. I need it sum all Meter Hrs values:
Meter Hrs is a measure defined as the following:
Meter Hrs Total =
CALCULATE(MIN('Tenna Hours'[total_hours]),LASTDATE('Tenna Hours'[import_date]))
- CALCULATE(MIN('Tenna Hours'[total_hours]),FIRSTDATE('Tenna Hours'[import_date]))
I thought this summing to zero, might be due to incorrect datatypes. [total_hours] as a datatype of Decimal Number and [import_date] has a datatype of Date/Time.
Try creating a SUMMARIZEd table:
Meter Hrs Total =
VAR MyTable =
ADDCOLUMNS(
SUMMARIZE( 'Tenna Hours', 'Tenna Hours'[Equipment] ),
"Meter Hrs",
CALCULATE(
MIN( 'Tenna Hours'[total_hours] ),
LASTDATE( 'Tenna Hours'[import_date] )
)
- CALCULATE(
MIN( 'Tenna Hours'[total_hours] ),
FIRSTDATE( 'Tenna Hours'[import_date] )
)
)
RETURN
IF(
ISFILTERED( 'Tenna Hours'[Equipment] ),
MINX( MyTable, [Meter Hrs] ),
SUMX( MyTable, [Meter Hrs] )
)

DATEADD throwing an error when adding MAX

I am looking to get the sum of sales for the last 3 months. not including the unfinished month which is this month.
I have this Dax but its not accepting the DATEADD with the MAX. Is there any workaround?
Measure1 = CALCULATE (
SUM ( table1[Sales] ),
DATESINPERIOD (
table1[date],
DATEADD( MAX ( table1[date] ), -1,MONTH),
-3,
MONTH
)
)
DATEADD function requires a set of dates to computation. That is why it not works with MAX, which returns single value. Find more here.
You need to find the maximum date in the dataset, then filter out the current month from the date table. Try the measure as follows:
Measure1 =
VAR maxDate =
CALCULATE(
MAX( 'Calendar'[Date] ),
ALL('Calendar' )
)
VAR firstDay = DATE( YEAR( maxDate ), MONTH( maxDate ), 1 )
VAR maxK =
CALCULATE(
MAX('Calendar'[Date] ),
'Calendar'[Date] < firstDay
)
VAR result =
CALCULATE(
SUM( AmountPaid[PaymentAmt] ),
DATESINPERIOD('Calendar'[Date], maxK, -3, MONTH )
)
return result
The maxK part calculates maximum date excluding the latest month in my dataset. You have to adjust the measure a bit for your needs (e.g. use TODAY() instead maxDate).
Hope it helps.

Hard coded filter in PowerBi measure

I have a measure (Users_1) that calculates number of rows between specific dates that have the parameter is_sql = 0.
This measure is used in a table alongside with other measures.
I have 5 more filters on the page that that should affect this specific measure and so I can not use All(users).
One of the filters on this page is "is_sql". And when is_sql = 1 every measure except for measure (Users_1) should change to correspondig value. Measure (Users_1) shold stay the same.
Now when I chose is_sql = 1 the measure (Users_1) is blank.
Users_1 =
CALCULATE(
COUNTROWS( 'users' ),
FILTER(
KEEPFILTERS('users'),
'users'[date (days)] <= MAX( 'Calendar'[Date] )
&&'users'[date (days)] >= MIN( 'Calendar'[Date] )
&&'users'[is_SQL] = 0
)
)
You will want to avoid using KEEPFILTERS on any filters you want to allow to look outside of the filter context:
Users_1 =
CALCULATE(
COUNTROWS( 'users' ),
'users'[date (days)] <= MAX( 'Calendar'[Date] ),
'users'[date (days)] >= MIN( 'Calendar'[Date] ),
'users'[is_SQL] = 0
)

Prev years YTD sales Dax formula

I am having some difficulties in creating a dax formula for calculating prev yr YTD sales.
I have written a formula but the same is not working.
I need to calculate the performance % yr over yr by comparing YTD sales of current year to YTD of prev yr sales.
any help would be appreciated
Sales sameperiod =
VAR first_date =
FIRSTDATE ( DATEADD ( 'Date'[Date], -12, MONTH ) )
VAR last_date =
LASTDATE ( DATEADD ( 'COGS Data'[Invoice Date], -12, MONTH ) )
RETURN
IF (
ISBLANK ( first_date ) || ISBLANK ( last_date ),
BLANK (),
CALCULATE (
SUM ( 'COGS Data'[Final Unit Cost] ),
DATESBETWEEN ( 'Date'[Date], first_date, last_date )
)
)
there are multiple ways, but my go-to is creating a Date table, I assume you already have it.
Then you would create relationship to Fact table from DateKey, and a new matrix visual with rows from Date Table, for example Date and Month. And Measure would be like -
Revenue last year = IF(
HASONEVALUE ('Date'[Month]),
IF (
SUM ('COGS Data'[Final Unit Cost] ) <> BLANK(),
CALCULATE (
SUM ( 'COGS Data'[Final Unit Cost] ),
SAMEPERIODLASTYEAR ('Date'[Date])
)
),
CALCULATE (
SUM ( 'COGS Data'[Final Unit Cost] ),
DATESBETWEEN (
'Date'[Date],
EDATE ( MIN ('Date'[Date]), -12 ),
EDATE ( MAX ('COGS Data'[Invoice Date]), -12 )
)

How to show DAX Cumulative Sum of Current Year and Previous Year on same visual?

I am trying to show the running totals (Rolling 12 months, not a calendar YTD) (cumulative sum) of revenue on the same graph (sharing the same date axis).
I created a measure for the current year cumulative sum that works fine:
$CumulativeBookingRevenueCY =
CALCULATE(
[$Revenue],
FILTER(
CALCULATETABLE(
SUMMARIZE(
'DimDateBooking',
'DimDateBooking'[Date]
),
ALL('DimDateBooking')
),
ISONORAFTER(
'DimDateBooking'[Date], MAX(DimDateBooking[Date]), DESC
)
)
)
But I can't figure out what is wrong with the previous year measure. This is the code I am using:
$CumulativeBookingRevenueLY =
CALCULATE(
[$Revenue LY],
FILTER(
CALCULATETABLE(
SUMMARIZE(
'DimDateBooking',
'DimDateBooking'[Date]
),
SAMEPERIODLASTYEAR('DimDateBooking'[Date])
),
ISONORAFTER(
SAMEPERIODLASTYEAR('DimDateBooking'[Date]), SAMEPERIODLASTYEAR(LASTDATE(DimDateBooking[Date])), DESC
)
)
)
Where my revenue measures are defined as follows:
$Revenue:= CALCULATE(Sum(FactTable[Revenue]))
$Revenue LY:= CALCULATE([$Revenue], DATEADD(DimDate[Date],-1,YEAR))
This is a sample of my data (CY is working, LY is not)
Can someone tell me what am I missing or doing wrong? Thank you!
Looks like you are missing a a few more steps.
You have:
$Revenue:= CALCULATE(Sum(FactTable[Revenue]))
$Revenue LY:= CALCULATE([$Revenue], DATEADD(DimDate[Date],-1,YEAR))
Use these additional measures below that incorporate the above measures, in your table:
revenue_last_year = IF( LASTDATE(Dates[Date]) > TODAY(), BLANK(), CALCULATE([$Revenue LY], DATESYTD(Dates[Date])))
revenue_this_year = IF( LASTDATE(Dates[Date]) > TODAY(), BLANK(), CALCULATE([$Revenue], DATESYTD(Dates[Date])))
Hope that helps!