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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
mathcathy
New Member

calculating from two date fields in one table

Hi,

 

I have a dataset which measures the start and end dates of many different activities.

To simplify, let's say three columns - Activity Type, Start Date, Completion Date

 

I want to create a new table with a count of the number of activities which are started and completed on each date.

So far, I've:

 - created a table with every date from the Start Date column and every activity type and then counted the number of rows with the correct start date and activity type into one column and the number of rows with the correct complete date and activity type into another. This didn't work because there were activities completed on dates where nothing started.

 - created a table with every date from the minimum start date to the maximum start date and counted the number of rows as above. This works to get the correct start and complete counts, but has no way to link to the activity type.

 

Can anyone suggest a way to get a table startpoint that has all the required dates associated to every possible activity type, please? (Or any alternate ideas to get the counts I'm looking for ...?)

 

Cathy

1 ACCEPTED SOLUTION
kdejonge
Employee
Employee

You can create a calculated table to solve this.

 

The expression would be something like this:

Table = var tablevalstart = SUMMARIZECOLUMNS(Table1[Start date],Table1[Activity type],"count", COUNTROWS(Table1))
var tablevalend = SUMMARIZECOLUMNS(Table1[End date],Table1[Activity type],"count", COUNTROWS(Table1))
var fulltable = UNION(tablevalstart,tablevalend)
return fulltable

 

This would first count all the occurences by start dates and activity type, then will do the same for End date and in the end Union them together. Now when you put this in a table or visual you will the sum of both each day by activity type.  

 

You can also do this using PowerQuery but for me DAX is faster :). Hope this is what you are looking for.

View solution in original post

6 REPLIES 6
kdejonge
Employee
Employee

You can create a calculated table to solve this.

 

The expression would be something like this:

Table = var tablevalstart = SUMMARIZECOLUMNS(Table1[Start date],Table1[Activity type],"count", COUNTROWS(Table1))
var tablevalend = SUMMARIZECOLUMNS(Table1[End date],Table1[Activity type],"count", COUNTROWS(Table1))
var fulltable = UNION(tablevalstart,tablevalend)
return fulltable

 

This would first count all the occurences by start dates and activity type, then will do the same for End date and in the end Union them together. Now when you put this in a table or visual you will the sum of both each day by activity type.  

 

You can also do this using PowerQuery but for me DAX is faster :). Hope this is what you are looking for.

I used:

Table = var tablevalstart = SUMMARIZECOLUMNS(Table1[Start date],Table1[Activity type],"count", COUNTROWS(Table1))
var tablevalend = SUMMARIZECOLUMNS(Table1[End date],Table1[Activity type],"count", COUNTROWS(Table1))
var fulltable = summarize(UNION(tablevalstart,tablevalend), [Start date], [Activity type])
return fulltable

 

This works. Is it a good way to have done it?

well you did lose the count, if that is OK then that'll work.

That works, because I needed the counts separately in order to subtract the completed items from the started items and get a picture of how much the queue size for each activity changes each day.

Those three columns were also a simplification of the original data, which has a couple of other columns which make things a little more complex.

 

Thank you so much for your help. I'd been puzzling on this one for a week or more.

Thank you. It's very very nearly what I need, except that the union query includes duplicates. Is there a way to join the two queries together which excludes duplicate dates and activity types?

well if you put this data in a table or visual you will not see the duplicated data, it will be grouped automatically. 

 

But if you really want it grouped you can create a new table with something like this:

Table 2 = SUMMARIZECOLUMNS('Table'[Activity type],'Table'[Start date],"count",SUM('Table'[count])) 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.