Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!
Hi all,
I'm looking for a solution to get the date difference between the current row and previous row for every unique client. Below is a very small example of what my dataset looks like for a single client. I would like to make a new column that calculates the difference for every client between his first row to the second row, the second to third row, etc.
Currently i'm trying to cook up something with the MAX or EARLIER dax functions, but not with much succes yet.
I'm curious to see what you guys can think up for this!
Thanks in advance,
Stan
Solved! Go to Solution.
Hi,
I solved the issue with some more research and luck. The sollution is as followed, after creating the Partition.Index as explained previously, I first create a column that calculates the previous end date.
Hi @StanTheMan ,
One sample for your reference, Please check the following steps.
1, Insert index column by id column. M code for your reference.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc5RCsAgDAPQu/Rb0HS6rWcR738NmUPaZX+hr5D0LpAkyMhaYCsebxxpW3UD2+3W2C43+9rpfeumsfiJ6v/K+4ythX1gtICVUf/rxwQ=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [id = _t, start = _t, end = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"end", type date}, {"start", type date}, {"id", Int64.Type}}), Partition = Table.Group(#"Changed Type", {"id"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}), #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"start", "end", "Index"}, {"Partition.start", "Partition.end", "Partition.Index"}), #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Partition",{{"Partition.Index", Int64.Type}, {"Partition.start", type date}, {"Partition.end", type date}}) in #"Changed Type1"
2. To create two calculated column to get the excepted result we need.
start = VAR ind = Table1[Partition.Index] - 1 VAR pre = CALCULATE ( MAX ( Table1[Partition.start] ), FILTER ( Table1, Table1[Partition.Index] = ind ), VALUES ( Table1[id] ) ) RETURN IF ( ISBLANK ( Table1[Partition.start] ), BLANK (), IF ( ind = 0, 0, DATEDIFF ( pre, Table1[Partition.start], DAY ) ) )
end = VAR ind = Table1[Partition.Index] - 1 VAR pre = CALCULATE ( MAX ( Table1[Partition.end] ), FILTER ( Table1, Table1[Partition.Index] = ind ), VALUES ( Table1[id] ) ) RETURN IF ( ISBLANK ( Table1[Partition.end] ), BLANK (), IF ( ind = 0, 0, DATEDIFF ( pre, Table1[Partition.end], DAY ) ) )
Hi @v-frfei-msft
Thanks for replying! At first glance the solution looked good to me, but upon further looking and trying to apply the solution on my own data set i've ran into some problems, see my screenshot. Can you help me by explaining the solution further?
I personally think its related to an error in my DAX Columns, they currently calculate way higher date differences than in your the example. (Directly copied your formulas and adjusted my own tablenames and columns) Maybe I'm misunderstanding what the columns calculate, so can you please explain to me what these columns calculate?
If i take my example from above, I'm expecting to see one new column that calculates the difference in days between the partition.index [1] -> partion.end [31-3-2018] and partion.index [2] -> partion.start [1-4-2018] = 1 day
I hope you can help me, thanks in advance!
Stan
Hi,
I solved the issue with some more research and luck. The sollution is as followed, after creating the Partition.Index as explained previously, I first create a column that calculates the previous end date.
User | Count |
---|---|
140 | |
113 | |
104 | |
77 | |
65 |
User | Count |
---|---|
135 | |
116 | |
101 | |
71 | |
61 |