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:
Related
I need to manipulate the Sales measure to exclude any transactions which have been reversed.
Sales measure is as follows right now:
Sales ($) =
IF (
HASONEVALUE ( 'Currency'[Detail] ),
SUMX (
'Sales',
'Sales'[Value_Sold]
* CALCULATE ( VALUES ( 'Exchange Rates'[ExchangeRate] ) )
)
)
What steps and where would I add to extract Reversed Transactions from this measure? I tried below as that's what you'd do in EXCEL but that is not working - when pulling results, it can't display anything on the visual
Sales - Reversals ($) =
IF (
HASONEVALUE ( 'Currency'[Detail] ),
SUMX (
'Sales',
'Sales'[Value_Sold]
* CALCULATE ( VALUES ( 'Exchange Rates'[ExchangeRate] ) ) - SUMX ('Sales', 'Sales [Trans_Type] = "Reversed")
)
)
)
If what you want is to exclude the reversed transactions from your first code, maybe this could help:
Sales ($) =
IF (
HASONEVALUE ( 'Currency'[Detail] ),
CALCULATE(
SUMX (
'Sales',
'Sales'[Value_Sold]
* CALCULATE (
VALUES ( 'Exchange Rates'[ExchangeRate] )
)
),
'Sales'[Trans_Type] <> "Reversed"
)
)
It's basically adding the SUMX into a CALCULATE function which let's you make the some applied to filters (in this case not reversed transactions).
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.
I need your help.
I am trying to make a calculated column which sum the sales for the last two dates (there are not sales on all dates!) for each product.
I have tried to use the calculation:
CALCULATE (
SUMX ( TOPN ( 2; 'Table'; 'Table'[date_ID] ); 'Table'[Sale] );
ALLEXCEPT ( Table; 'Table'[Product_ID] )
)
But this only works if you have one sale per date per product ID.
What do I do if there are many transactions per date and prod ID?
Below I have a table where I have (filtered) one date and one product.
Now, I have made a calculated column that sums all transactions per date and product ID (for reference).
This is
Calculated Column 2 =
CALCULATE (
SUMX ( Table; Table[Sale] );
ALLEXCEPT ( Table; 'Table'[Date]; 'Table'[Product_ID] )
)
Now comes the difficult part.
If I want to sum the transactions for the last two dates on each product, I have made the following calculation:
Calculated Column 1 =
VAR SumProd =
CALCULATE (
SUMX ( Table; Table[Sale] );
ALLEXCEPT ( Table; Table[Date]; Table[Product_ID] )
)
RETURN
CALCULATE (
SUMX ( TOPN ( 2; 'Table'; 'Table'[Date] ); SumProd );
ALLEXCEPT ( Table; Table[Product_ID]; Table[Date] )
)
The Problems:
The calculations sums ALL the values in each category
Example: in the table, you see "Calculated column 1"=7571200, which is 27040 * number of transactions? I only want the value 27040.
For some reason, the TOPN() doesn’t work. If I change the N_Value=2 to N_Value=3, the calculation doesn’t change?
Please, does anyone know what is wrong with my calculation?
Thanks.
Br,
Jakob
In your measure, SumProd is a constant that you're summing for each row in the table. This is not what you want.
I'd suggest something more like this:
Calculated Column 1 =
VAR LastTwoDates =
CALCULATETABLE (
VALUES ( Table[Date] );
TOPN ( 2; ALLEXCEPT ( Table; Table[Product_ID] ); Table[Date] )
)
RETURN
CALCULATE (
SUM ( Table[Sale] );
ALLEXCEPT ( Table; Table[Product_ID] );
Table[Date] IN LastTwoDates
)
In this, we first calculate a list of the top 2 dates associated with Product_ID and store it as a variable. Then we use that as a filter when calculating the sum of Sale.
I'm not positive this exact syntax will work, but I hope it will point you in a better direction.
It turns out my code didn't work exactly as intended. Try this instead:
Calculated Column 1 =
VAR ProductDates =
CALCULATETABLE (
VALUES ( Table1[Date] );
ALLEXCEPT ( Table1; Table1[Product_ID] )
)
VAR LastTwoDates = TOPN ( 2; ProductDates; [Date] )
RETURN
CALCULATE (
SUM ( Table1[Sale] );
ALLEXCEPT ( Table1; Table1[Product_ID] );
Table1[Date] IN LastTwoDates
)
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 )
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.