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
ylebec
Regular Visitor

Formula to count occurrences based on dynamic time-frame (slicer)

Hi,

I'm quite experienced in Excel but new to Power BI and DAX. After some research I could not find anything that would help me with my case.

 

We have operated 198 bus trips (or bus services) last Friday (Weekday), 163 last Saturday and 141 last Sunday. What we operated usually differs from what it is scheduled, i.e. 209 on Weekdays, 171 on Saturdays and 160 on Sundays. This is due to sometimes not being able to operate everything that's scheduled for the day (Go Public Transport! 🙂 )

 

I have two tables, one with the scheduled services

scheduled.PNG

and one with the operated ones by date:

Operated.PNG

 

Now as you can see the Trip Key metric (a unique identifier for a bus trip) is common to both tables so I have them joined (1 to Many from Scheduled to Operated)

Joined.PNG

Problem, when I extend the timeframe to include more weekdays as per above table, the scheduled total number remains being 540 (1x Wd + 1x Sat + 1xSun trips), getting a false total for the selected data range.

 

What I need is a formula to populate in this case 4x209 + 1x171 + 1x160 = 1167 trips scheduled (vs 1071 operated).

 

Any ideas?

Thanks a lot

2 REPLIES 2
bidevsugmen
Resolver I
Resolver I

Hi @ylebec

 

You can consider the below steps to get the expected result.

I have created 2 tables based on the sample data provided by you. Table 1 is the master table and table 2 contains actual number of trips done.

 

1. In query editor, create custom column (named as index) in table 1.image.png

2. In table 2, create duplicate of service column. image.png

 

 

3. Split the duplicate column based on first occurrence of space.image.png

 

4. Create custom column (named as index) in this table also.image.png

 

5.Merge table 1 into table 2 based on Index column and expand it.image.png

 

6. This is the final output of table 2. 

image.png

 

You can now pull the columns highlighted in the above image into the table visualization. 

And this should give you the expected output as below.

Please do accept it as solution if it works!

 image.png

 

 

 

Thanks,

Suguna.

 

 

 

Hi Suguna

 

Thank you for the great tutorial to implement this solution! One thing though... the real master table with the scheduled trips contains over 11K rows! whereas the Operated Services one close to a Million or more, not really sure as these tables are pulled from an Access DB. What I showed on the example was just a minor fraction of the data to simplify.

Now, everything goes according to plan until I have to 'Expand' the table after merging it... due to the insane size it's taking forever.

 

I had thought of creating a relationship between both tables based on Index and then pull the columns without the expansion... but due to none of them having unique values it is not doable.

 

Any alternatives?

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.