Rolling 20 workday revenue - powerbi

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.

Related

more filter apply on the DAX

Good Day!
Request
The following is my raw data, with some conditions applied to check which columns need to be counted.
Example:
Row 1, direction is export, department code is not starting with 'D',
will count all 6 columns (ETD, ATD,ETA, ATA, Estimated Delivey,
Actual Delivery) , and only 5 have been filled in, so get 83 as the
percentage.
Row 2, direction is export, department code starting
with 'D', will only count 4 columns (ETA, ATA,Estimated Delivery
and Actual Delivery), and only 2 has been filled in, so get 50%.
What I have now:
I have a code but it only shows all columns and which column has been filled, and I would like some help in calculating the conditions as stated above.
DAX: (calculate not blank)
Count =
SUMX(
ADDCOLUMNS(
RawData,
"Count",
var tab = {RawData[ETD],RawData[ATD],RawData[ETA],RawData[ATA],RawData[Estimated Delivery],RawData[Actual Delivery]}
var result =
COUNTROWS(
FILTER(
tab,
[Value]<>BLANK()))
return
IF(
ISBLANK(result),
0,
result
) ),[Count])
DAX: Calculate blank
Count =
SUMX(
ADDCOLUMNS(
RawData,
"Count",
var tab = {RawData[ETD],RawData[ATD],RawData[ETA],RawData[ATA],RawData[Estimated Delivery],RawData[Actual Delivery]}
var result =
COUNTROWS(
FILTER(
tab,
[Value]=BLANK()))
return
IF(
ISBLANK(result),
0,
result
) ),[Count])
Any help will be greatly appreciated.
Attached here with my pbix: https://drive.google.com/file/d/1KIROrAzNEp710JEfxMfiZLzvTpuHj3OZ/view?usp=sharing
Thank you!
For the count I added the decision making of the "D";
Count =
SUMX(
ADDCOLUMNS(
RawData,
"Count",
var Dep = RawData[Dep]
var res1 = COUNTROWS(
FILTER(
{RawData[ETD],RawData[ATD],RawData[ETA],RawData[ATA],RawData[Estimated Delivery],RawData[Actual Delivery]},
NOT ISBLANK([Value])
)
)
var res2 = COUNTROWS(
FILTER(
{RawData[ETA],RawData[ATA],RawData[Estimated Delivery],RawData[Actual Delivery]},
NOT ISBLANK([Value])
)
)
return if (LEFT(Dep, 1) = "D", res1, res2)
),
[Count]
)
I also followed your logic to go with the Counts measure:
Counts =
SUMX(
ADDCOLUMNS(
RawData,
"Count",
var Dep = RawData[Dep]
var res1 = COUNTROWS(
FILTER(
{RawData[ETD],RawData[ATD],RawData[ETA],RawData[ATA],RawData[Estimated Delivery],RawData[Actual Delivery]},
ISBLANK([Value])
)
)
var res2 = COUNTROWS(
FILTER(
{RawData[ETA],RawData[ATA],RawData[Estimated Delivery],RawData[Actual Delivery]},
ISBLANK([Value])
)
)
return if (LEFT(Dep, 1) = "D", res1, res2)
),
[Count]
)
Then I added the measure %
% = RawData[Count]/RawData[Total]
Result:
I do believe you would be better of using calculated columns but did not go their as you had choosen measures already.
IF I would have started from scratch, I would have gone with an unpivot table in power query what makes the coding more dynamic

How do I get the period difference for each item in the table in PowerBI desktop?

As seen below, I need to find the difference in quantities for each individual fruits with respect to the previous value. I have previously tried using in built Power BI functions like "PreviousDay()" but haven't found success yet.
You can create these below 2 Measure for your purpose-
Pervious_Quantity =
Var current_row_date = MIN(your_table_name[Date])
var previous_date =
CALCULATE(
MAX(your_table_name[Date]),
FILTER(
ALLEXCEPT(your_table_name, your_table_name[Fruit]),
your_table_name[Date] < current_row_date
)
)
RETURN
CALCULATE(
SUM(your_table_name[Quantity]),
FILTER(
ALLEXCEPT(your_table_name, your_table_name[Fruit]),
your_table_name[Date] = previous_date
)
)
Difference = min(your_table_name[Quantity]) - if([Pervious_Quantity] = BLANK(),0, [Pervious_Quantity])
Here is the final output-

DAX Creating Calendar Table With Additional Field

I'm creating a calendar table in DAX:
Dates =
CALENDAR (
MIN ( 'Work Weeks'[Start Date].[Date] ),
MAX ( 'Work Weeks'[Start Date].[Date] )
)
The work weeks table contains a week number and a start date for each week.
For each date in my new Dates table, I want to assign a work week number using the start date of the work week. Note that work weeks start on different days of the week (they're assigned properly in my work weeks table though).
So what I'm trying is:
Dates =
ADDCOLUMNS (
CALENDAR (
MIN ( 'Work Weeks'[Start Date].[Date] ),
MAX ( 'Work Weeks'[Start Date].[Date] )
),
"Work Week",
CALCULATE (
MAX ( 'Work Weeks'[Start Date].[Date] ),
'Work Weeks'[Start Date] <= Date
)
)
I'm not sure how to reference the current row/date in the condition at the end. And then I also need to return the work week number, rather than just the start date.
Assuming your work week table looks something like this:
You can use this date table code to add the week number:
Date Table =
VAR ListOfDate =
VAR MinDate = MIN ( Sales[Order Date] ) -- Replace the reference with the column of your model
VAR MaxDate = MAX ( Sales[Order Date] ) -- Replace the reference with the column of your model
VAR StartDate = DATE ( 2021, 1, 1 ) -- DATE ( YEAR ( MinDate ), 1, 1 )
VAR EndDate = DATE ( 2021, 12, 31 ) -- DATE ( YEAR ( MaxDate ), 12, 31 )
VAR Result = CALENDAR ( StartDate, EndDate )
RETURN
Result
VAR WorkWeekTable = ALL ( WorkWeek )
VAR CalendarTable =
GENERATE (
ListOfDate,
VAR CurrentDate = [Date]
RETURN
ROW (
"Calendar Year Number", YEAR ( CurrentDate ),
"Calendar Month Number", MONTH ( CurrentDate ),
"Work week",
CALCULATE (
MIN ( WorkWeek[Work Week Number] ),
CurrentDate >= WorkWeek[Work Week Start Date],
CurrentDate <= WorkWeek[Work Week End Date],
REMOVEFILTERS ( )
)
)
)
RETURN
CalendarTable
The result will look like this:

Max of date, based on different column

i'd like to identify the most recent date of one column, grouped by other column.
i want to identify my bigger "Valuation date" based on "Policy Effective Date", and if is possible assign 1 if is the most recent date, and 0 if not, i couldn't group by dates ,
this is my formula now:
_z_ValDate =
CALCULATE(
MAX(vwLossRunData[ValuationDate]),
ALLEXCEPT(vwLossRunData,vwLossRunData[EffectiveDate])
)
Thanks !!
What you can do is create a column:
_z_ValDate =
var curDate = vwLossRunData[Policy Effective Date]
return CALCULATE(MAX(vwLossRunData[Valuation date]), FILTER(vwLossRunData, curDate = vwLossRunData[Policy Effective Date]))
It takes the max of the [Valuation Date] from the filtered table by [Policy Effective Date]
You can try this below Measure-
_z_ValDate =
VAR current_row_pe_date = MIN(vwLossRunData[Policy Effective Date])
VAR max_v_date =
CALCULATE(
MAX(vwLossRunData[ValuationDate]),
FILTER(
ALLSELECTED(vwLossRunData),
vwLossRunData[Policy Effective Date] = current_row_pe_date
)
)
RETURN IF(current_row_pe_date = max_v_date, 1 0)

DAX: Reset a cumulative total by certain date thresholds

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)