Forum Discussion
Count for every month in the date interval
- 6 years ago
Hi TrulsB
Create a date table ( this date table has no relationship with your table)
DATE = ADDCOLUMNS ( CALENDARAUTO (), "year", YEAR ( [Date] ), "quarter", SWITCH ( TRUE, MONTH ( [Date] ) IN { 1, 2, 3 }, "Q1", MONTH ( [Date] ) IN { 4, 5, 6 }, "Q2", MONTH ( [Date] ) IN { 7, 8, 9 }, "Q3", MONTH ( [Date] ) IN { 10, 11, 12 }, "Q4" ), "monthname", FORMAT ( [Date], "mmm" ), "monthno", FORMAT ( [Date], "yyyy-mm" ), "week", WEEKNUM ( [Date], 2 ) )Create calcualted columns in this date table
start of month = STARTOFMONTH('DATE'[Date]) end of month = ENDOFMONTH('DATE'[Date]) start of quarter = STARTOFQUARTER('DATE'[Date]) end of quarter = ENDOFQUARTER('DATE'[Date]) start of year = STARTOFYEAR('DATE'[Date]) end of year = ENDOFYEAR('DATE'[Date])Add columns from Date table to the X-axis of the visual as below
Then create measures
day level = CALCULATE ( DISTINCTCOUNT ( 'Table'[machine] ), FILTER ( 'Table', 'Table'[Start date] <= MAX ( 'DATE'[Date] ) && 'Table'[Stop date] >= MAX ( 'DATE'[Date] ) ) ) month level = CALCULATE ( DISTINCTCOUNT ( 'Table'[machine] ), FILTER ( 'Table', 'Table'[Stop date] >= MAX ( 'DATE'[start of month] ) && 'Table'[Start date] <= MAX ( 'DATE'[end of month] ) ) ) quarter level = CALCULATE ( DISTINCTCOUNT ( 'Table'[machine] ), FILTER ( 'Table', 'Table'[Stop date] >= MAX ( 'DATE'[start of quarter] ) && 'Table'[Start date] <= MAX ( 'DATE'[end of quarter] ) ) ) year level = CALCULATE ( DISTINCTCOUNT ( 'Table'[machine] ), FILTER ( 'Table', 'Table'[Stop date] >= MAX ( 'DATE'[start of year] ) && 'Table'[Start date] <= MAX ( 'DATE'[end of year] ) ) ) Final result = IF ( ISINSCOPE ( 'DATE'[Date] ), [day level], IF ( ISINSCOPE ( 'DATE'[week] ), [day level], IF ( ISINSCOPE ( 'DATE'[monthname] ), [month level], IF ( ISINSCOPE ( 'DATE'[quarter] ), [quarter level], [year level] ) ) ) )Best Regards
MaggieCommunity Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi TrulsB
Do you have tables like this?
| Title | Start date | Stop date |
| "Service on production machine A. | 1/1/2020 | 1/3/2020 |
| "Service on production machine B. | 1/10/2020 | 3/28/2020 |
You want to calculate counts for machine A,B,C..,ect in date periods (year,month,week,,ect), right?
i have a doubt that you say "The production machine A is not available in the service period (between the start and stop date)",
is this mean the counts should not include the machine A?
But "for the example this service job should be included in the number of service jobs, for the whole time periode",
I don't understand well about your statement and requirements.
Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.