cancel
Showing results for
Did you mean:

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Frequent Visitor

## Find period with last x incidents

Hey all

Been working with PBI a couple of years and can often find similar issues in here that helps me - but this time I'm stuck... So hope some one is able to help

I have three tables

Case-table

Absense-table

Calender

In the case-table I have a measure that counts the days that each case has been open. In the absense-table I have a similar measure that counts the days of absense

There are no relation between the calender-table and the two others

 Date ID absense case 01-01-2023 22 1 02-01-2023 22 1 03-01-2023 22 04-01-2023 22 1 1 05-01-2023 23 1 1 06-01-2023 23 1 07-01-2023 23 08-01-2023 23 1 1 09-01-2023 23 1

For each ID I need to find the period that contains the last 365 days where the case has been open. The period can be longer than one year, as there can be days where the case is not open. At the same time I need the sum of absense-days in the same period, and divide these.

For exampel: The period containing the last 365 case-days are 1/1-22 to 1/3-23 (425 days). In that period there has been 90 absense-days = 90/365... 25%

Any ideas how to solve this with Dax

Thanks

R_

1 ACCEPTED SOLUTION
Super User

you can modify the query to produce date lists per ID. Then you can use these to intersect with the case dates for each ID.

It was a proof of concept of implementing UNIONX in Power Query.

10 REPLIES 10
Super User

Please show the expected outcome based on the sample data you provided.

Frequent Visitor

I hope this exampel provides the nessesary information

Super User

The case days computation is relatively trivial

``````Case Days =
var b = ADDCOLUMNS(Calender,"case",if(SELECTEDVALUE('case'[End date])>=[Date] && SELECTEDVALUE('case'[Start date])<=[Date],1,0))
var c = topn(365,filter(b,[case]=1),[Date],DESC)
return sumx(c,[case])``````

However the Absense days computation is tricky because DAX still does not have a UNIONX operator. (Please vote for the idea).  Still  thinking.

Frequent Visitor

Thanks - the case days computation works. Keeping the post open and hope someone have an idea to solve the other part 🙂

Super User

Does it have to be DAX or would Power Query be acceptable?

Frequent Visitor

Power Query could be an option

The case start date and case end date could also be a part of the absense-table - if that makes it possible solving it in dax?

Super User

No, that won't help.  Only UNIONX would help.

Here's the Power Query version.

``````let
Kilde = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI00DUw0jUyMDICcoyAHBMYxzGpODWvOFUpVgei0sBQF6gYKgnSZgjiGAM5AYnFxZllEJVgSVMkY4AcQyM8ZkKMAXGMgRxjGAeuMhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Start date" = _t, #"End date" = _t, Kolonne1 = _t]),
#"Omdøbte kolonner" = Table.RenameColumns(Kilde,{{"Kolonne1", "Type"}}),
#"Ændret type" = Table.TransformColumnTypes(#"Omdøbte kolonner",{{"Start date", type date}, {"End date", type date}}),
#"Added Custom" = Table.AddColumn(#"Ændret type", "Range", (k)=> List.Generate(()=>Number.From(k[Start date]),each _<=Number.From(k[End date]),each _+1)),
#"Expanded Range" = Table.ExpandListColumn(#"Added Custom", "Range"),
res = List.Difference(Table.SelectRows(#"Expanded Range",each [Type]="Absense")[Range],Table.SelectRows(#"Expanded Range",each [Type]="Passive")[Range]),
#"Converted to Table" = Table.FromList(res, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", type date}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Column1", "Date"}})
in
#"Renamed Columns"``````
Frequent Visitor

Hmm - maybe I don't get it 😕

The query ends with one single date collum - how can I achieve the result per ID with these dates?

Super User

you can modify the query to produce date lists per ID. Then you can use these to intersect with the case dates for each ID.

It was a proof of concept of implementing UNIONX in Power Query.

Frequent Visitor

Aah - okay. I will give it a go

Thanks for all your help Ibendlin

Announcements

#### Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

#### Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

#### Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors