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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Data_Scrubber
Frequent Visitor

How to create calculated column with count of distinct items per date & location?

Hi,

 

I'm trying to add a calculated column to my dataset that counts the number of distinct serial numbers produced per day, and per site.

  • This value will be used as a global filter in the PBIX file, so it'll only show data for days where there are greater than X serial numbers.
  • It needs to be a filter, because different teams have different min values they want to use.

 

I have a decent amount of Power BI/DAX experience. This seems conceptually simple, but I'm WAY overthinking it, and that's led me down many PBI Community/stackoverflow rabbit holes 🙂 A nudge in the right direction would be greatly appreciated!

 

I think I need to do a group by with just the columns I want to aggregate on. This might be the right configuration for the groupby:

 

GROUPBY(Append2, Append2[Date], Append2[Site], "SN Count Per Day and Site", COUNTX(CURRENTGROUP(), DISTINCTCOUNT(Append2[Serial Number] )))

 

 

When I run that in the DAX Query View, it returns a table with 3 columns - date, site, SN count per day and site.

[Date][Site][SN Count Per Day and Site]
Thu Dec 01 2022 Site #23
Thu Dec 01 2022 Site #120
Fri Dec 02 2022 Site #115
Sat Dec 03 2022 Site #13
Tue Dec 06 2022 Site #213
Tue Dec 06 2022 Site #114
Wed Dec 07 2022 Site #215
Wed Dec 07 2022 Site #110
Thu Dec 08 2022 Site #18
Thu Dec 08 2022 Site #214

 

It's the values in the SN Count Per Day and Site column that I want to include in the new calculated column in my dataset. So for all twenty rows in the data set with a date of 12/1/2022 and site # of 1, they should all show the value 20.


Question #1: How do I do that? Is a JOIN the right approach? I've never done that before in DAX, but have in SQL. But that seems overly complex, so I think there's a concept I'm missing.

 

Question #2: For the GROUPBY - I'm confused on why I need to use COUNTX to aggregate here, but that's what appeared to work when I was playing around with the formula. The aggregation I want is the distinct # of serial numbers produced for that date/site combo. I believe COUNTX here would be the # of rows with a given date/site combo - which could mean duplicate serial numbers.

 

(I read the awesome FAQ before posting, and did my best on brevity)

1 ACCEPTED SOLUTION
Rupak_bi
Resolver II
Resolver II

Try this syntax

Calculate(distinctcount ( table, sl no), allexcept( table, site, date))

View solution in original post

2 REPLIES 2
Rupak_bi
Resolver II
Resolver II

Try this syntax

Calculate(distinctcount ( table, sl no), allexcept( table, site, date))

Hi Rupak,

 

I greatly appreciate your prompt reply - sorry I was slow to respond.

 

That worked perfectly. I'm a little confused by how ALLEXCEPT works, but I think I just need to go process more examples for it to sink in.

 

THANK YOU!

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors