DATEADD throwing an error when adding MAX - powerbi

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.

Related

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 )
)

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] )
)

Cumulative measure with a conditional reset

I have the need of creating a cumulative measure that resets when a certain condition happens.
The target is to create the 'Longest Stock Out Period' measure. That will be inserted in a matrix visual like this.
Date
No. of Negative Days
Longest Stock Out Period
08-03-2022
0
0
09-03-2022
1
1
10-03-2022
1
2
11-03-2022
0
0
The logic is that 'Longest stock out period' should cumulative sum 'No. of negative days' until 'No. of negative days' is 0, then it should reset.
This is what I have currently tried, which just computes a 0. I believe there should also be some logic in the measure regarding no. of negative days should not be 0 or alike.
Longest Stock Out =
VAR _date =
SELECTEDVALUE ( 'Calendar'[Date] )
RETURN
CALCULATE (
[No of Days],
FILTER ( ALL ( 'Calendar'[Date] ), 'Calendar'[Date] <= _date )
)
Try this Measure:
Longest Stock Out Period :=
VAR ThisDate =
MIN( 'Table'[Date] )
VAR DateOfLatestZero =
CALCULATE(
MAX( 'Table'[Date] ),
FILTER(
ALL( 'Table' ),
'Table'[Date] <= ThisDate
&& 'Table'[No. of Negative Days] = 0
)
)
RETURN
CALCULATE(
SUM( 'Table'[No. of Negative Days] ),
FILTER(
ALL( 'Table' ),
'Table'[Date] <= ThisDate
&& 'Table'[Date] >= DateOfLatestZero
)
)
Adapt so as to use your Calendar dates as required.

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 )
)

YTD Average of Measure

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.