Forum Discussion
Count rows with datediff previous date and actual date between 30 days
I have this kind of data table with DeviceNr, TaskNr and date:
| DeviceNr | TaskNr | Date |
| D001 | T-001 | 01.01.2020 |
| D001 | T-001 | 02.01.2020 |
| D001 | T-002 | 25.01.2020 |
| D001 | T-003 | 27.01.2020 |
| D001 | T-003 | 30.01.2020 |
| D002 | T-004 | 05.01.2020 |
| D002 | T-005 | 03.02.2020 |
| D002 | T-006 | 07.02.2020 |
| D002 | T-006 | 10.03.2020 |
| D002 | T-007 | 13.03.2020 |
| D002 | T-008 | 17.04.2020 |
| D003 | T-009 | 05.01.2020 |
| D003 | T-010 | 08.02.2020 |
| D003 | T-010 | 10.02.2020 |
| D003 | T-011 | 14.02.2020 |
I want to get this result:
The result measure should count the rows with the condition: "if the datediff between the previous average date an the actual average date is <= 30 days it should count the row."
The average date is calculated in the data model with:
=AVERAGEX(FILTER('tab_tasks';'tab_tasks'[TaskNr]=EARLIER('tab_tasks'[TaskNr]));'tab_tasks'[Date])
The previous date is calculated with:
=CALCULATE(MAX('tab_tasks'[average date per task]);FILTER(ALL(tab_tasks[TaskNr];'tab_tasks'[average date per task]);COUNTROWS(FILTER('tab_tasks';EARLIER(tab_tasks[average date per task])<'tab_tasks'[average date per task]))))
Average Date = CALCULATE(average(tab_tasks[Date]),ALLEXCEPT(tab_tasks,tab_tasks[TaskNr],tab_tasks[DeviceNr])) Prev Avg Date = var t = tab_tasks[TaskNr] var pt = CALCULATE(max(tab_tasks[TaskNr]),ALLEXCEPT(tab_tasks,tab_tasks[DeviceNr]),tab_tasks[TaskNr]<t) return CALCULATE(AVERAGE(tab_tasks[Date]),ALLEXCEPT(tab_tasks,tab_tasks[DeviceNr]),tab_tasks[TaskNr]=pt) Ct = var c = CALCULATE(COUNTROWS(tab_tasks),ALLEXCEPT(tab_tasks,tab_tasks[DeviceNr],tab_tasks[TaskNr])) return SWITCH(TRUE(),ISBLANK(tab_tasks[Prev Avg Date]),BLANK(),datediff(tab_tasks[Prev Avg Date],tab_tasks[Average Date],DAY)<31,divide(1,c,0),0)
7 Replies
- lbendlin
Super User
Note: the below are calculated columns as you didn't indicate if the result can be impacted by user filter choices and you didn't indicate what to do with the DeviceNr.
Average Date = CALCULATE(average(tab_tasks[Date]),ALLEXCEPT(tab_tasks,tab_tasks[TaskNr])) Prev Avg Date = var t = tab_tasks[TaskNr] var pt = CALCULATE(max(tab_tasks[TaskNr]),ALL(tab_tasks),tab_tasks[TaskNr]<t) return CALCULATE(AVERAGE(tab_tasks[Date]),ALL(tab_tasks),tab_tasks[TaskNr]=pt) Ct = if(datediff(tab_tasks[Prev Avg Date],tab_tasks[Average Date],DAY)<31,1,0)I leave the cleanup decisions up to you. Technically a single date per task can still yield an average.
- AnonymousNot applicable
Hello, thank you for your reply.
It is very important, that the subtotals will be calculated right, as the sum of the column ct.
If I want to copy your measure "Prev Avg Date", I get an error message: "Not a single value can be determined for column TaskNr".
Maybe there is another solution approach.
Best regards - lbendlin
Super User
Please provide sample data that covers such a scenario.
- AnonymousNot applicable
Thank you for your reply. In the following table is the new example date for the table 'tab_tasks'
DeviceNrTaskNrDateD001T-0CE01.01.2020D001T-0CE02.01.2020D001T-0WB25.01.2020D001T-0CR27.01.2020D001T-0CR30.01.2020D002T-0VW05.01.2020D002T-0BM03.02.2020D002T-0WY07.02.2020D002T-0WY10.03.2020D002T-0LL13.03.2020D002T-0UU17.04.2020D003T-0EW05.01.2020D003T-0IU08.02.2020D003T-0IU10.02.2020D003T-0PT14.02.2020
- lbendlin
Super User
Average Date = CALCULATE(average(tab_tasks[Date]),ALLEXCEPT(tab_tasks,tab_tasks[TaskNr],tab_tasks[DeviceNr])) Prev Avg Date = var t = tab_tasks[TaskNr] var pt = CALCULATE(max(tab_tasks[TaskNr]),ALLEXCEPT(tab_tasks,tab_tasks[DeviceNr]),tab_tasks[TaskNr]<t) return CALCULATE(AVERAGE(tab_tasks[Date]),ALLEXCEPT(tab_tasks,tab_tasks[DeviceNr]),tab_tasks[TaskNr]=pt) Ct = var c = CALCULATE(COUNTROWS(tab_tasks),ALLEXCEPT(tab_tasks,tab_tasks[DeviceNr],tab_tasks[TaskNr])) return SWITCH(TRUE(),ISBLANK(tab_tasks[Prev Avg Date]),BLANK(),datediff(tab_tasks[Prev Avg Date],tab_tasks[Average Date],DAY)<31,divide(1,c,0),0)
- AnonymousNot applicable
Thank you for your reply. In the following table is the new example date for the table 'tab_tasks'
There are some issues with the HTML Code for tables.
DeviceNr TaskNr Date
D001 T-0CE 01.01.2020 00:00:00
D001 T-0CE 02.01.2020 00:00:00
D001 T-0WB 25.01.2020 00:00:00
D001 T-0CR 27.01.2020 00:00:00
D001 T-0CR 30.01.2020 00:00:00
D002 T-0VW 05.01.2020 00:00:00
D002 T-0BM 03.02.2020 00:00:00
D002 T-0WY 07.02.2020 00:00:00
D002 T-0WY 10.03.2020 00:00:00
D002 T-0LL 13.03.2020 00:00:00
D002 T-0UU 17.04.2020 00:00:00
D003 T-0EW 05.01.2020 00:00:00
D003 T-0IU 08.02.2020 00:00:00
D003 T-0IU 10.02.2020 00:00:00
D003 T-0PT 14.02.2020 00:00:00
- lbendlin
Super User
See my reply above. I already used your new sample data.