DAX Calculated Measure based on Month - powerbi

I have a Opening Count Measure (Given Below). The Logic which I have applied is:
If you looking for the Opening Count for Jan-2017 then Merge1[Loggd_dt] should be before 01 Jan 2017 and Merge1[Cmplt_dt] values should be from 01 jan 2017 (The data is from year 2005 upto date and the logic is to be applied to overall data). I am taking this measure in a matrix(along with two other measures). I have also applied some slicer in the same page. My other two measures are changing according to the value selected in Slicer. But the measure given below is not changing according to slicer.
Measure 1:
Opening Count =
IF (
HASONEVALUE ( DateTable[StartOfMonthDate] ),
CALCULATE (
[Count_rows],
FILTER (
ALL ( Merge1 ),
(
Merge1[LOGGD_DT]
< CALCULATE (
VALUES ( DateTable[StartOfMonthDate] ),
USERELATIONSHIP ( DateTable[Date], Merge1[LOGGD_DT] )
)
&& Merge1[CMPLT_DT]
>= CALCULATE (
VALUES ( DateTable[StartOfMonthDate] ),
USERELATIONSHIP ( DateTable[Date], Merge1[CMPLT_DT] )
)
)
)
)
)
Measure 2:
Closed Count =
CALCULATE (
[Count_rows],
USERELATIONSHIP ( DateTable[Date], Merge1[CMPLT_DT] ),
Merge1[STATUS] = "CL"
)
Measure 3:
InProcess Count =
CALCULATE (
[Count_rows],
USERELATIONSHIP ( DateTable[Date], Merge1[LOGGD_DT] )
)
Measure used:
Count_rows = COUNTROWS(Merge1)

The problem is probably the ALL Function. I think it overrides the filter in your slicer.
Try ALLEXCEPT ( 'Merge1'[put slicer-column here] ) in stead of ALL ( Merge1 )

Related

Same column Date difference based on other field DAX POWER BI

I want to calculate the date difference for all values in Field1 based on Field2 for example datediff(3/1/2020-2/1/2020) based on Field2 (c-b).
I have tried several ways but no luck so far.
there is no minimum mentioned in the question, so i assume that you are looking for the difference of the previous stages.
Date Difference =
DATEDIFF (
CALCULATE (
MAX ( 'Table'[Date] ),
FILTER (
ALL ( 'Table' ),
'Table'[Date] < EARLIER ( 'Table'[Date] )
&& EARLIER ( 'Table'[Field1] ) = 'Table'[Field1]
)
),
'Table'[Date],
DAY
)
You can obtain a solution by adding 2 calculated columns:
First Calculated Column:
MinDate =
CALCULATE (
MIN ( YourTable[Date] ),
ALLEXCEPT ( YourTable, YourTable[Field1] )
)
2nd Calculated Column:
Date_Difference =
DATEDIFF ( [MinDate], [Date], DAY )
End Result:

Power BI matrix grand totals wont add up

I'm new to Power BI and facing this issue with a Power BI report grand totals of these columns won't add up. Any help is much appreciated, below are the formulas
Rolling 3 Months =
CALCULATE (
SUM ( Deliveries[NetRevenue] ),
DATESBETWEEN (
Deliveries[DeliveryDate],
MAX ( Deliveries[DeliveryDate] ) - 90,
MAX ( Deliveries[DeliveryDate] )
)
)
Prior 3 Months =
CALCULATE (
SUM ( Deliveries[NetRevenue] ),
DATESBETWEEN (
Deliveries[DeliveryDate],
MAX ( Deliveries[DeliveryDate] ) - 180,
MAX ( Deliveries[DeliveryDate] ) - 90
)
)
Screenshot of the result
This is likely because MAX ( Deliveries[DeliveryDate] ) is not the same for every row.
The maximum is evaluated within the local filter context, not over the entire Deliveries table (or the subset of the table that matches your filter settings).
I'm guessing you probably want to define a variable to use as your date rather than calculating it (potentially) differently for each row in your matrix. E.g.
Prior 3 Months =
VAR LastDate =
CALCULATE ( MAX ( Deliveries[DeliveryDate] ), ALLSELECTED ( Deliveries ) )
RETURN
CALCULATE (
SUM ( Deliveries[NetRevenue] ),
DATESBETWEEN ( Deliveries[DeliveryDate], LastDate - 180, LastDate - 90 )
)
You might be able to use TODAY() instead of that LastDate calculation, depending on your particular situation.

Power BI: Measure is not ignoring Slicer Filter

My Dax Query is correctly bringing back the expected data. The one caveat, I can't seem to ignore an outside slicer to the visual for this one particular calculation. What am I doing wrong?
xxPY_TrafficSum = CALCULATE (
[xPY_TrafficSum],
DATESBETWEEN (
DimDate[FullDate],
DATE (
YEAR ( ALLSELECTED ( DimDate[FullDate] ) ) - 2,
MONTH ( ALLSELECTED ( DimDate[FullDate] ) ),
DAY ( ALLSELECTED ( DimDate[FullDate] ) ) + 1
),
DATE (
YEAR ( ALLSELECTED ( DimDate[FullDate] ) ) - 1,
MONTH ( ALLSELECTED ( DimDate[FullDate] ) ),
DAY ( ALLSELECTED ( DimDate[FullDate] ) ) + 1
)
),
ALL ( DimDate[IsLastDayOfMonth], DimDate[IsLastDayOfMonth] )
)
I don't think you have to specify the column twice within the ALL function. Although I am not sure how this would affect the calculation, you can give it a try:
All(DimDate[IsLastDayOfMonth])
One more thing you could try is to move the position of the ALL function within the calculation. i.e) switch the positions of the DATESBETWEEN and the ALL functions. If your issue is still not resolved, it would be great if you can share a sample data/file for us to look at.

Running total of a measure which use DATESINPERIOD and months from fact table

I am using a measure below to display the months from fact table as described here:
Billings12Months =
CALCULATE (
SUM ( 'Datatable'[Allowable] ),
DATESINPERIOD ( DimDate[Date], MIN ( DimDate[Date] ), +12, MONTH )
)
My attempt to get the running total of above measure is failing:
BillingsRunningTotal =
CALCULATE (
[Billings12Months],
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
BillingsRunningTotal2 =
SUMX (
FILTER (
ALLSELECTED ( DimDate[Date] ),
DimDate[Date] <= MAX ( ( DimDate[Date] ) )
&& YEAR ( DimDate[Date] ) = YEAR ( MIN ( DimDate[Date] ) )
),
[Billings12Months]
)
[BillingsRunningTotal] return same values as [Billings12Months] (please see screen 1 attached) and
[BillingsRunningTotal2] return wrong values and month start from Jan, 17 instead of May, 17 (please see screen-2)
Please help me to calculate the running total. If possible please describe how your solution is working so that I can be better in DAX.
Update:
Please see the screen-3 below for the output when I use the measure suggested by Kosuke:
BillingsRunningTotal =
CALCULATE (
SUM ( Datatable[Allowable] ),
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
The months are from fact table (not from a Date table) and I think DATESINPERIOD plays a role to calculate and display the months. When we use SUM ( Datatable[Allowable] ), there would be a single month as dictated by the slicer. So we need to use DATESINPERIOD with rolling month calculation logic (DimDate[Date] <= MAX ( DimDate[Date] )) or virtually sum the [Billings12Months], It is where I am failing.
Thanks
You are almost there with the first attempt, however what to calculate is not [Billings12Months], but SUM( Datatable[Allowable] ).
BillingsRunningTotal =
CALCULATE (
SUM ( Datatable[Allowable] ),
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
Essentially, [Billings12Months] and [BillingsRunningTotal] are same in calculating the sum of Datatable[Allowable], but the only difference is each measure calculates for different scope of period. Therefore, the right way of thinking is to wrap SUM ( Datatable[Allowable] ) in CALCULATE, with different filter parameters.

How do I calculate the sum of Value for the last 6 sprints using DAX

Problem:
I need a calculated measure in DAX that sums the Value column for the last 6 sprints. I am basing the last 6 sprints on the DimSprintEndDateKey in descending order.
Table structure in PowerBI
The DAX that I am using:
CALCULATE (
SUM ( factSprint[Value] ),
FILTER (
ALL ( factSprint ),
COUNTROWS (
topn(6,
FILTER (
factSprint,
EARLIEST( RELATED ( dimSprint[DimSprintEndDateKey] ) )
> RELATED ( dimSprint[DimSprintEndDateKey] )
),RELATED ( dimSprint[DimSprintEndDateKey] ), DESC
)
)
)
)
I am assuming that the relationship on your tables is between 'dimSprint'[dimSprintKey] and 'FactSprint'[dimSprintKey].
That being the case, this measure could work for you.
Total Value Last Six Sprints =
VAR endDateSprint =
LOOKUPVALUE (
'dimSprint'[dimSprintEndDateKey],
'dimSprint'[dimSprintKey], SELECTEDVALUE ( 'FactSprint'[dimSprintKey] )
)
VAR dimTableFiltered =
FILTER ( 'dimSprint', 'dimSprint'[dimSprintEndDateKey] <= endDateSprint )
RETURN
CALCULATE (
SUM ( 'FactSprint'[Value] ),
ALL ( 'FactSprint' ),
TOPN ( 6, dimTableFiltered, [dimSprintEndDateKey], DESC )
)
Use it on a matrix visual (or pivottable). Be sure to put 'FactSprint'[dimSprintKey] or 'FactSprint'[SprintPK] on Rows and [Total Value Last Six Sprints] on Values.