Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Anonymous
Not applicable

Creating dynamic measures to calculate duration between different timestamps

Dear all,

 

I have a database with logs of clients going through different stages of a process.

Each process has timestamp indicating the date when the single client has reached that stage of the process.

grgmssgnn_0-1707322748255.png

I want to create in DAX a dynamic measure that would allow users to choose the stages they want to calculate a lag time between. E.g.: one might be interested to investigate how many days (on average) elapse between date 1 and date 0, or between date 2 and date 0, and so on...

 

I tried using field parameters for start date and arrival date, in order for the user to just choose in a filter the starting point and the arrival point of the measure, which would be ideal for me. Nonetheless, it seems I can't be able to get this to work.

 

How would you proceed?

 

Thanks in advance for your kind help.

Giorgio

1 ACCEPTED SOLUTION
Anonymous
Not applicable

HI @Anonymous,

I'd like to suggest you convert the table records to do complex unpivot column operation on these status and date fields:

1.png

Full query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZRNa8MwDIb/Ssi5EMtxvo6jO42xlV1LD2YpmyGLuzbpYb9+UVEhzqTMhxwiCz1+rVfa71NIN6kqMgWZVjqffnb+4gZ39RTX93hC39b3l2N/GW2XPLvv0bV2cL6fDrTOlLlnP5xOZ3+1g8UyeTZVuh0cNvtUTyGoeSJUDPFpbN27sx2WkhgQMnLKjWY87l6pCi+hCsobzDV8eZ2vS9DFjPHi+8TOZYScAtEgyKgZzpv78u3Y2Z+pJUeE1ZKgIgCVGBJ6orj3WoJUIYB0qKjC3FJQxL3cH0VGckATgGoMCQ5Ap8b7GhvAE8uA2JDVo5s1MwWYFVOgkWccUOQiVpphQDtne5+0Ljm7Yz+cfdK5j8+BrmvC7BsBqBJrh//8XcX7G/TKILHbJ97gEJJy6jvbHRVDknYD3n9OMiskzZFk54nbLpxewD2hpanizB49vstnLInONgzExSrXXxiiWlncrLepPqyYDh99zqgpFq1Bnp+Gn5+G5iB6nYoE7OqScPgF", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SNDG = _t, #"Data 0: Inizio monitoraggio" = _t, #"1: Contatto cliente" = _t, #"Data 1: Contatto cliente" = _t, #"2: Rintraccio" = _t, #"Data 2: Rintraccio" = _t, #"3: Analisi posizione e condivisione strategia" = _t, #"Data 3: Analisi posizione e condivisione strategia" = _t, #"4: Delibera" = _t, #"Data 4: Delibera" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"SNDG", Int64.Type}, {"Data 0: Inizio monitoraggio", type text}, {"1: Contatto cliente", type text}, {"Data 1: Contatto cliente", type text}, {"2: Rintraccio", type text}, {"Data 2: Rintraccio", type text}, {"3: Analisi posizione e condivisione strategia", type text}, {"Data 3: Analisi posizione e condivisione strategia", type text}, {"4: Delibera", type text}, {"Data 4: Delibera", type text}}),
    fxTrans= (tb as table) =>
        let 
            #"Unpivoted Columns" = Table.UnpivotOtherColumns(tb, {}, "Attribute", "Value"),
            #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "Step", each Text.Combine(List.Select(Text.ToList([Attribute]), each Value.Is(Value.FromText(_), Int32.Type )))),
            #"Grouped Rows2"= Table.Group(#"Added Custom", {"Step"}, {{"Count", each Table.Transpose(Table.SelectColumns(_,{"Value"})), type table }}),
            #"ExpandTableColumn" = Table.ExpandTableColumn(#"Grouped Rows2", "Count", {"Column1", "Column2"}, {"Status", "Date"})
        in
            #"ExpandTableColumn",
    #"Grouped Rows" = Table.Group(#"Changed Type", {"SNDG", "Data 0: Inizio monitoraggio"}, {{"Content", each fxTrans(Table.RemoveColumns(_,{"SNDG", "Data 0: Inizio monitoraggio"})), type table }}),
    #"Expanded Content" = Table.ExpandTableColumn(#"Grouped Rows", "Content", {"Step", "Status", "Date"}, {"Step", "Status", "Date"})
in
    #"Expanded Content"

After these steps, you can simply use Dax expression with current group field value(SNDG) and steps as conditions. Then you can use them to find out correspond date values and calculated in DATEDIFF function.

Regards,

Xiaoxin Sheng

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Here is the dummy data I'm starting from.

As you can see, there are several steps of the process, and each step has a datestamp (Data 1, Data 2, Data 3, etc.)
My measure would ideally use DATEDIFF or a similar function but the starting and end datestamps should be variable or subject to user decision.

 

Thanks for your help!

SNDGData 0: Inizio monitoraggio1: Contatto clienteData 1: Contatto cliente2: RintraccioData 2: Rintraccio3: Analisi posizione e condivisione strategiaData 3: Analisi posizione e condivisione strategia4: DeliberaData 4: Delibera
105/01/2023Positivo05/02/2023  Consensual Liquidation22/04/2023Approvata03/05/2023
218/01/2023Positivo17/02/2023  Judicial02/04/2023Approvata13/05/2023
302/01/2023Positivo17/02/2023  DPO13/04/2023Approvata07/05/2023
414/01/2023Positivo23/02/2023  Judicial25/04/2023Non approvata17/05/2023
501/01/2023Positivo18/02/2023  Rimodulazione28/04/2023Approvata05/05/2023
608/01/2023Positivo07/02/2023  Rimodulazione05/04/2023Approvata27/05/2023
706/01/2023Positivo13/02/2023  Rimodulazione24/04/2023Approvata19/05/2023
812/01/2023Positivo22/02/2023  Consensual Liquidation01/04/2023Approvata16/05/2023
903/01/2023Positivo18/02/2023  Judicial14/04/2023Non approvata02/05/2023
1025/01/2023Positivo24/02/2023  Piano di rientro light03/04/2023  
1124/01/2023Positivo03/02/2023  Judicial27/04/2023Non approvata17/05/2023
1214/01/2023Positivo05/02/2023  Rimodulazione28/04/2023Approvata01/05/2023
1322/01/2023Positivo10/02/2023  Rimodulazione23/04/2023Approvata14/05/2023
1422/01/2023Positivo20/02/2023  Consensual Liquidation02/04/2023Approvata15/05/2023
1526/01/2023Positivo12/02/2023  Rimodulazione05/04/2023Approvata01/05/2023
1615/01/2023Positivo01/02/2023  DPO05/04/2023Approvata07/05/2023
1702/01/2023Positivo24/02/2023  DPO01/04/2023Non approvata20/05/2023
1820/01/2023Positivo01/02/2023  Piano di rientro light09/04/2023  
1927/01/2023Positivo07/02/2023  Piano di rientro light26/04/2023  



 

Anonymous
Not applicable

HI @Anonymous,

I'd like to suggest you convert the table records to do complex unpivot column operation on these status and date fields:

1.png

Full query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZRNa8MwDIb/Ssi5EMtxvo6jO42xlV1LD2YpmyGLuzbpYb9+UVEhzqTMhxwiCz1+rVfa71NIN6kqMgWZVjqffnb+4gZ39RTX93hC39b3l2N/GW2XPLvv0bV2cL6fDrTOlLlnP5xOZ3+1g8UyeTZVuh0cNvtUTyGoeSJUDPFpbN27sx2WkhgQMnLKjWY87l6pCi+hCsobzDV8eZ2vS9DFjPHi+8TOZYScAtEgyKgZzpv78u3Y2Z+pJUeE1ZKgIgCVGBJ6orj3WoJUIYB0qKjC3FJQxL3cH0VGckATgGoMCQ5Ap8b7GhvAE8uA2JDVo5s1MwWYFVOgkWccUOQiVpphQDtne5+0Ljm7Yz+cfdK5j8+BrmvC7BsBqBJrh//8XcX7G/TKILHbJ97gEJJy6jvbHRVDknYD3n9OMiskzZFk54nbLpxewD2hpanizB49vstnLInONgzExSrXXxiiWlncrLepPqyYDh99zqgpFq1Bnp+Gn5+G5iB6nYoE7OqScPgF", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SNDG = _t, #"Data 0: Inizio monitoraggio" = _t, #"1: Contatto cliente" = _t, #"Data 1: Contatto cliente" = _t, #"2: Rintraccio" = _t, #"Data 2: Rintraccio" = _t, #"3: Analisi posizione e condivisione strategia" = _t, #"Data 3: Analisi posizione e condivisione strategia" = _t, #"4: Delibera" = _t, #"Data 4: Delibera" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"SNDG", Int64.Type}, {"Data 0: Inizio monitoraggio", type text}, {"1: Contatto cliente", type text}, {"Data 1: Contatto cliente", type text}, {"2: Rintraccio", type text}, {"Data 2: Rintraccio", type text}, {"3: Analisi posizione e condivisione strategia", type text}, {"Data 3: Analisi posizione e condivisione strategia", type text}, {"4: Delibera", type text}, {"Data 4: Delibera", type text}}),
    fxTrans= (tb as table) =>
        let 
            #"Unpivoted Columns" = Table.UnpivotOtherColumns(tb, {}, "Attribute", "Value"),
            #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "Step", each Text.Combine(List.Select(Text.ToList([Attribute]), each Value.Is(Value.FromText(_), Int32.Type )))),
            #"Grouped Rows2"= Table.Group(#"Added Custom", {"Step"}, {{"Count", each Table.Transpose(Table.SelectColumns(_,{"Value"})), type table }}),
            #"ExpandTableColumn" = Table.ExpandTableColumn(#"Grouped Rows2", "Count", {"Column1", "Column2"}, {"Status", "Date"})
        in
            #"ExpandTableColumn",
    #"Grouped Rows" = Table.Group(#"Changed Type", {"SNDG", "Data 0: Inizio monitoraggio"}, {{"Content", each fxTrans(Table.RemoveColumns(_,{"SNDG", "Data 0: Inizio monitoraggio"})), type table }}),
    #"Expanded Content" = Table.ExpandTableColumn(#"Grouped Rows", "Content", {"Step", "Status", "Date"}, {"Step", "Status", "Date"})
in
    #"Expanded Content"

After these steps, you can simply use Dax expression with current group field value(SNDG) and steps as conditions. Then you can use them to find out correspond date values and calculated in DATEDIFF function.

Regards,

Xiaoxin Sheng

Anonymous
Not applicable

Hi @Anonymous ,

Can you please share a pbix or some dummy data that keep the raw data structure with expected results? It should help us clarify your scenario and test to coding formula.

How to Get Your Question Answered Quickly  

Regards,

Xiaoxin Sheng

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.