Forum Discussion
Average Lag in DAX from SQL Server
- 6 years ago
Alright thanks I see what you are getting at. I created a calculated table with the following DAX (your table is referenced as Deliveries, your column names are the same:
LagTable = VAR tmpTable = FILTER(Deliveries, Deliveries[Deliveries] > 0) VAR tablePrevDate = ADDCOLUMNS(tmpTable, "PrevDate", MAXX(FILTER(tmpTable, [AddressId] = EARLIER([AddressId]) && [DeliveredDateCT] < EARLIER([DeliveredDateCT])), [DeliveredDateCT])) VAR tableLags = ADDCOLUMNS(FILTER(tablePrevDate, NOT(ISBLANK([PrevDate]))), "Lag", DATEDIFF([PrevDate], [DeliveredDateCT],DAY)) RETURN SUMMARIZE(tableLags, [AddressId], "AvgLag", AVERAGEX(FILTER(tableLags, [AddressId] = EARLIER([AddressId])), [Lag]))This can be a lot simpler but I wanted to clearly show the logic.
VAR tmpTable: First we filter your table to filter out the 0 deliveries (they are noise).VAR tablePrevDate: We add a column to tmpTable, by taking the max date in a filtered tmpTable (filtered on current AddressID and Date < current row context date)
VAR TableLags: Adds a column to a filtered tablePrevDate (which is filtered to remove everything without a prevDate value) and add a Lag calculation
RETURN: a summary table per AddressID, with a column AvgLag.Let me know if this works for you! 🙂
Kind regards
Djerro123
-------------------------------
If this answered your question, please mark it as the Solution. This also helps others to find what they are looking for.
Keep those thumbs up coming! 🙂
Can you provide some sample data and expected output? I don't fully follow your SQL query to be honest.
Kind regards
Djerro123
-------------------------------
If this answered your question, please mark it as the Solution. This also helps others to find what they are looking for.
Keep those thumbs up coming! 🙂
- stevengaris6 years agoFrequent Visitor
The inner query is pulling all the delivery dates and finding their previous delivery date then the outer query is averaging all the date diffs for each location to find out on average how frequently they get deliveries.
The expected result would be the average for each location.
- JarroVGIT6 years agoResident Rockstar
Alright thanks I see what you are getting at. I created a calculated table with the following DAX (your table is referenced as Deliveries, your column names are the same:
LagTable = VAR tmpTable = FILTER(Deliveries, Deliveries[Deliveries] > 0) VAR tablePrevDate = ADDCOLUMNS(tmpTable, "PrevDate", MAXX(FILTER(tmpTable, [AddressId] = EARLIER([AddressId]) && [DeliveredDateCT] < EARLIER([DeliveredDateCT])), [DeliveredDateCT])) VAR tableLags = ADDCOLUMNS(FILTER(tablePrevDate, NOT(ISBLANK([PrevDate]))), "Lag", DATEDIFF([PrevDate], [DeliveredDateCT],DAY)) RETURN SUMMARIZE(tableLags, [AddressId], "AvgLag", AVERAGEX(FILTER(tableLags, [AddressId] = EARLIER([AddressId])), [Lag]))This can be a lot simpler but I wanted to clearly show the logic.
VAR tmpTable: First we filter your table to filter out the 0 deliveries (they are noise).VAR tablePrevDate: We add a column to tmpTable, by taking the max date in a filtered tmpTable (filtered on current AddressID and Date < current row context date)
VAR TableLags: Adds a column to a filtered tablePrevDate (which is filtered to remove everything without a prevDate value) and add a Lag calculation
RETURN: a summary table per AddressID, with a column AvgLag.Let me know if this works for you! 🙂
Kind regards
Djerro123
-------------------------------
If this answered your question, please mark it as the Solution. This also helps others to find what they are looking for.
Keep those thumbs up coming! 🙂
- stevengaris6 years agoFrequent Visitor
Wowzer you're a genius!!!! I have never used a calculated table before but that does exactly what I need it to do! Thanks a million!!