Max of date, based on different column - if-statement

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)

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 to get the Top N vales based on a column for each category in PowerBI?

I am facing the issue while filtering out the data based on a "Date" column to fetch top 3 for each category. Below is the sample data:
Can anybody help me with this to get the below-expected output?
You can try this (here dummy data); You can choice ASC or DESC based on your need:
Ranking by Date = var _cat = SELECTEDVALUE( Sheet1[Category])
return
IF(RANKX(FILTER(ALL(Sheet1), Sheet1[Category]= _cat), calculate(MAX(Sheet1[Date])),,ASC ,Skip)<=2, 1,BLANK())
or by Sale:
Ranking by Sales =
IF (
ISINSCOPE ('Sheet1'[Date] ),
VAR ProductsToRank = 2
VAR SalesAmount = [SumOf]
RETURN
IF (
SalesAmount > 0,
VAR VisibleProducts =
CALCULATETABLE (
VALUES ( 'Sheet1' ),
ALLSELECTED ( 'Sheet1'[Date] )
)
VAR Ranking =
RANKX (
VisibleProducts,
[SumOf],
SalesAmount
)
RETURN
IF (
Ranking > 0 && Ranking <= ProductsToRank,
1
)
)
)
Or you can create a new table in DAX like this:
Top2 = GENERATE(VALUES(Sheet1[Category]), TOPN(2, FILTER(SELECTCOLUMNS(ALL(Sheet1[Category], Sheet1[Date]),"Cat",[Category],"Date",[Date]),[Cat] = [Category]),[Date]))

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:

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.