DAX: Reset a cumulative total by certain date thresholds - powerbi

I have a cumulative total of CE's CE's cumulative (see below).
I need the cumulative total to reset to 0 after hitting the ResetDate
Looked all around forums but unable to find the answer, hope you can help me out.
CE's cumulative =
CALCULATE(
SUM (CE's),
FILTER( ALL( DimDate),
DimDate[Date] <= MAX( DimDate[Date] )
)
)
Date CE's cumulative ResetDate
10-10-2019 77.670.099
11-10-2019 78.057.691 11-10-2019
12-10-2019 78.114.554
13-10-2019 78.234.181
14-10-2019 78.469.789
15-10-2019 78.709.015 15-10-2019
16-10-2019 80.070.020

You can use this below measure-
cumulative_sum =
VAR current_date = MIN(your_table_name[Date])
VAR last_reset_date =
CALCULATE(
MAX(your_table_name[ResetDate]),
FILTER(
ALL(your_table_name),
your_table_name[Date] <= current_date
)
)
RETURN
IF(
last_reset_date = BLANK(),
CALCULATE(
SUM(your_table_name[CE's cumulative]),
FILTER(
ALL(your_table_name),
your_table_name[Date] <= current_date
)
),
CALCULATE(
SUM(your_table_name[CE's cumulative]),
FILTER(
ALL(your_table_name),
your_table_name[Date] <= current_date
&& your_table_name[Date] >= last_reset_date
)
)
)
Here is the output-

I would find the closest ResetDate to my Date and then use that in the filter:
VAR currentDate = SELECTEDVALUE(dimDate[date])
VAR lastResetDate = LASTDATE(FILTER(ALL(dimDate[ResetDate], dimDate[ResetDate] < currentDate)))
RETURN CALCULATE(SUM(CE's),
ALL(dimDate),
dimDate[date] > lastResetDate && dimDate[date] <= currentDate)

Related

DAX Summarize - drops records on group by

Source Data:
DAX:
Total Derived =
VAR selected_min_date =
MIN ( 'Date'[Date] )
VAR selected_max_date =
MAX ( 'Date'[Date] )
VAR vTable =
SUMMARIZE (
FILTER (
MyTable,
MyTable[PDate] >= selected_min_date
&& MyTable[PDate] <= selected_max_date
),
MyTable[Gcode],
MyTable[DerTax]
)
var result = sumx(vTable, MyTable[DerTax])
RETURN
Result
I was expecting result as 2644.48, but is outputting 1322.24
What am doing wrong.
I was same result same as with below sql.
Select GCode,
sum(DerTax)
from MyTable
where PDate >= #datemin and PDate <=#datemax
group by GCode
I don't think you've grasped how SUMMARIZE works. Since your Gcode and DerTax entries are identical for each of the two rows, your SUMMARIZE statement will generate a table comprising just a single row, viz:
Gcode
DerTax
Grp01
1322.24
Instead of passing DerTax as a GroupBy column, you should be passing an aggregation of that field:
Total Derived: =
VAR selected_min_date =
MIN( 'Date'[Date] )
VAR selected_max_date =
MAX( 'Date'[Date] )
VAR vTable =
SUMMARIZE(
FILTER(
MyTable,
MyTable[PDate] >= selected_min_date
&& MyTable[PDate] <= selected_max_date
),
MyTable[Gcode],
"Tax", SUM( MyTable[DerTax] )
)
VAR result =
SUMX(
vTable,
[Tax]
)
RETURN
Result

Create column "after" in Powerbi (DAX)

I have the following information and I want to create the column "Later" From isProm : is the next day have the same value or no?
Date isProm Later
2018-06-06 1 1
2018-06-13 1 1
2018-08-20 1 1
2018-09-12 1 0
2018-09-12 0 0
Could you help me to do that with day please?
Thank you very much,
Ana
Create your new Custom Column with this below code-
Option 1:
later =
var current_row_isporm = your_table_name[isProm]
var current_row_date = your_table_name[Date]
var next_date =
CALCULATE(
MIN(your_table_name[Date]),
FILTER(
ALL(your_table_name),
your_table_name[Date] > current_row_date
)
)
var nex_date_isporm =
CALCULATE(
MIN(your_table_name[isProm]),
FILTER(
ALL(your_table_name),
your_table_name[Date] = next_date
)
)
RETURN IF(current_row_isporm = nex_date_isporm,1,0)
Option 2: You can also use this below code for same output-
later =
var current_row_isporm = your_table_name[isProm]
var current_row_date = your_table_name[Date]
var next_date_isporm =
CALCULATE(
MINX(
TOPN(
1,
FILTER(
ALL(your_table_name),
your_table_name[Date] > current_row_date
),
your_table_name[Date].[Date],ASC
),
your_table_name[isProm]
)
)
RETURN IF(current_row_isporm = next_date_isporm,1,0)
Here is the output. I have slightly different output because of date format in my laptop.

DAX: Mapping items if appeared both in 2 dates

How can I map or count new items that were also in my data the previous month?
For example:
ID / date / item
123 / 01.12.20 / one
123 / 01.11.20 / one
143 / 01.11.20 / two
153 / 01.12.20 / three
Will get:
123 / one
It looks like the Item is the description of the ID.
We can write a measure that shows the Item if it was also present in the preious month or blank otherwise. This way, creating a Table visual in Power BI with the ID and this measure we will just see the IDs and Items that where present in the previous month.
Item if in previous month =
IF(
HASONEVALUE( T[Item] )
&& HASONEVALUE( T[ID] ),
VAR currentMonth =
MONTH(
MAX( T[Date] )
)
VAR currentItem =
SELECTEDVALUE( T[Item] )
RETURN
IF(
NOT ISEMPTY(
FILTER(
ALL( T ),
MONTH( T[Date] ) = CurrentMonth - 1
&& T[Item] = currentItem
)
),
currentItem
)
)
This works if we have two months of data, like in your question. If we have more than two months, then maybe that what we really want is to check the previous month of the last one.
Item if in previous to last month =
VAR LastMonth =
MONTH(
MAXX(
ALL( T ),
T[Date]
)
)
RETURN
IF(
HASONEVALUE( T[Item] )
&& HASONEVALUE( T[ID] )
&& MONTH(
MAX( T[Date] )
) = LastMonth,
VAR currentItem =
SELECTEDVALUE( T[Item] )
RETURN
IF(
NOT ISEMPTY(
FILTER(
ALL( T ),
MONTH( T[Date] ) = LastMonth - 1
&& T[Item] = currentItem
)
),
currentItem
)
)

How to enhance combination code with a time variable

I found the following code by v-shex-msft I would like to use to come up with a list of combinations. Kudos to him. Now I would like to add a time variable so that only events are combined that occurred within 24 hours. Unfortunately, the filter function won't allow me to use more arguments:
Original Code:
Summary Table =
var temp=
SUMMARIZE(
Sheet5,
Sheet5[Customer],
"Combinations",CONCATENATEX(
FILTER(
SUMMARIZE(
Sheet5,
[Customer],
Sheet5[Type]
),
Sheet5[Customer] = EARLIER(Sheet5[Customer])
),
[Type]&","
)
)
return
SUMMARIZE(
temp,
[combinations],
"Number",COUNTAX(
FILTER(
temp,
[combinations]=EARLIER([combinations])
),
[Customer]
)
)
I tried to add the time variable as follows:
Summary Table =
var temp =
SUMMARIZE(
Sheet5,
Sheet5[Customer],
"Combinations",CONCATENATEX(
FILTER(
SUMMARIZE(
Sheet5,
[Customer],
Sheet5[Type],
Sheets5[time]
),
Sheet5[Customer] = EARLIER(Sheet5[Customer])
&& Sheets5[time]+1 >= Earlier(Sheets5[time])
&& Sheets5[time]-1 <= Earlier(Sheets5[time])
),
[Type]&","
)
)
return
SUMMARIZE(
temp,
[combinations],
"Number",COUNTAX(
FILTER(
temp,
[combinations] = EARLIER([combinations])
),
[Customer]
)
)
Any Ideas on what is going wrong? Thank you your help is much appreciated.
Can you try this below code-
Summary Table =
var temp =
SUMMARIZE(
Sheet5,
Sheet5[Customer],
"Combinations",CONCATENATEX(
FILTER(
SUMMARIZE(
Sheet5,
[Customer],
Sheet5[Type],
Sheets5[time]
),
MAX(Sheet5[Customer]) = EARLIER(Sheet5[Customer])
&& MAX(Sheets5[time]) +1 >= Earlier(Sheets5[time])
&& MAX(Sheets5[time]) -1 <= Earlier(Sheets5[time])
),
[Type]&","
)
)
return
SUMMARIZE(
temp,
[combinations],
"Number",COUNTAX(
FILTER(
temp,
[combinations] = EARLIER([combinations])
),
[Customer]
)
)

Rolling 20 workday revenue

Currently visualizing sales for the past 30days, but looking to switch it in to the past 20 workdays instead, I've got workday column up and running in datetable, so ideally id want to use a filter of workday=1 and grab the 20 newest rows?
Sales 30d =
CALCULATE([Sales],
FILTER(
ALL(d_dates[date]),
d_dates[date]
>TODAY()-30))
This is what im using to show revenue for past 30 days, what'll i need to change?
You can try with this below measure-
slaes_last_20_days =
VAR today = TODAY()
VAR selected_date_min =
MINX(
TOPN(
20,
FILTER(
ALL(d_dates),
d_dates[date].[Date] <= today
&& workday = 1
),
d_dates[date].[Date],
DESC
),
d_dates[date].[Date]
)
RETURN
CALCULATE(
[Sales],
FILTER(
ALL(d_dates),
d_dates[date].[Date] >= selected_date_min
&& workday = 1
)
)
VAR Last20Workdays =
selectcolumns(
TOPN(
20,
FILTER(
d_dates,
d_dates[date] < TODAY()
&& d_dates[workday] = 1
),
d_dates[date],
DESC
),
"WorkDay",
d_dates[date]
)
This worked.