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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Count customers in a period

Spoiler
Hi there,

I am finding me lost in counting customers for a period. My data looks like this:

lost_flower_2-1673641032419.png

 

Well, I can count manually, but I have millions of data and have no good idea. Is there a function in Power BI which can calculate how many customers we have in which month?
lost_flower_1-1673641009240.png

 


thank you 

 

2 ACCEPTED SOLUTIONS
adudani
Super User
Super User

@Anonymous ,

 

try pasting the below code into a blank query.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TYy5DcAwDAN3UW1AFJ3A8CyCijz7zxAZzlcej6S7bFLEoFiVsJ6AptUSCIniso9oUXBGWaYmv/6Ye2s/T37+HPuusOc/fb0h4gI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [customer = _t, startdate = _t, enddate = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"customer", type text}, {"startdate", type date}, {"enddate", type date}}),
Period = Table.AddColumn(#"Changed Type", "Period", each Duration.Days([enddate] - [startdate]), Int64.Type),
#"Dates in Period" = Table.AddColumn(Period, "Dates in Period", each List.Dates([startdate],[Period],#duration(1, 0, 0, 0))),
#"Expanded Dates in Period" = Table.ExpandListColumn(#"Dates in Period", "Dates in Period"),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Dates in Period",{{"Dates in Period", type date}}),
#"Inserted Month" = Table.AddColumn(#"Changed Type1", "Month", each Date.MonthName([Dates in Period])),
#"Inserted Year" = Table.AddColumn(#"Inserted Month", "Year", each Date.Year([Dates in Period]), Int64.Type),
MOnthYear = Table.AddColumn(#"Inserted Year", "MOnthYear", each Text.Combine({Text.From([Year], "en-CA"), Text.From([Month], "en-CA")}), type text),
#"Removed Columns" = Table.RemoveColumns(MOnthYear,{"startdate", "enddate", "Period", "Dates in Period", "Month", "Year"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Columns"),
#"Grouped Rows" = Table.Group(#"Removed Duplicates", {"MOnthYear"}, {{"DistinctCount Customer", each Table.RowCount(Table.Distinct(_)), Int64.Type}})
in
#"Grouped Rows"

 

 

you can apply a sort order to the month year column as well.

 

Appreciate a thumbs up if this helps.

 

Please accept this as a solution if your question is resolved.

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash

View solution in original post

Hi @Anonymous 

 

Your "Total_Customer" table is a calculated table while a calculated table/column cannot show in Power Query Editor. So you cannot use Power Query to get the count from this calculated table. Instead, you have to use DAX to get the result. 

 

You can add a Date table which has YearMonth column into the model and ensure it is not connected to the calculated table. Then create the following measure.

Measure = COUNTROWS(FILTER('Table','Table'[StartDate]<=MAX('Date'[Date]) && 'Table'[EndDate]>=MAX('Date'[Date])))

 

Add YearMonth column and above measure into a table visual. 

vjingzhang_0-1673848256223.png

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Hi adudani,

 

I was able to create the new query with your code. Thanks so much.

 

My original data is in a table

lost_flower_0-1673715756390.png

 

 

I am not sure how I can change your code so that it uses my "Total customer" table.

 

Can you please help me again?

 

Thank you

@Anonymous ,

 

Are you able to share the files removing sensitive information?

 

The reason is because this is a calcuted table.

 

Else, i would've suggested you update the source step of the solution and this would apply. ( After making required changes in the changed type step)

 

Let me know thanks

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash
Anonymous
Not applicable

Hi adudani,

I have tried to update the source as my data also comes from a calculated table / field

lost_flower_3-1673781748790.png

 

and I replaced the blue marked part

lost_flower_1-1673781300506.png

with the table name "Total_Customer"

lost_flower_2-1673781413027.png

 

Seems not to be that easy as I was hoping. Is it even possible to use an existing table as a source?

 

Hi @Anonymous 

 

Your "Total_Customer" table is a calculated table while a calculated table/column cannot show in Power Query Editor. So you cannot use Power Query to get the count from this calculated table. Instead, you have to use DAX to get the result. 

 

You can add a Date table which has YearMonth column into the model and ensure it is not connected to the calculated table. Then create the following measure.

Measure = COUNTROWS(FILTER('Table','Table'[StartDate]<=MAX('Date'[Date]) && 'Table'[EndDate]>=MAX('Date'[Date])))

 

Add YearMonth column and above measure into a table visual. 

vjingzhang_0-1673848256223.png

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

Anonymous
Not applicable

Thank you so much. This helps me to understand my issue better. I should get it done now.

 

Biggest thanks ever

 

adudani
Super User
Super User

@Anonymous ,

 

try pasting the below code into a blank query.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TYy5DcAwDAN3UW1AFJ3A8CyCijz7zxAZzlcej6S7bFLEoFiVsJ6AptUSCIniso9oUXBGWaYmv/6Ye2s/T37+HPuusOc/fb0h4gI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [customer = _t, startdate = _t, enddate = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"customer", type text}, {"startdate", type date}, {"enddate", type date}}),
Period = Table.AddColumn(#"Changed Type", "Period", each Duration.Days([enddate] - [startdate]), Int64.Type),
#"Dates in Period" = Table.AddColumn(Period, "Dates in Period", each List.Dates([startdate],[Period],#duration(1, 0, 0, 0))),
#"Expanded Dates in Period" = Table.ExpandListColumn(#"Dates in Period", "Dates in Period"),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Dates in Period",{{"Dates in Period", type date}}),
#"Inserted Month" = Table.AddColumn(#"Changed Type1", "Month", each Date.MonthName([Dates in Period])),
#"Inserted Year" = Table.AddColumn(#"Inserted Month", "Year", each Date.Year([Dates in Period]), Int64.Type),
MOnthYear = Table.AddColumn(#"Inserted Year", "MOnthYear", each Text.Combine({Text.From([Year], "en-CA"), Text.From([Month], "en-CA")}), type text),
#"Removed Columns" = Table.RemoveColumns(MOnthYear,{"startdate", "enddate", "Period", "Dates in Period", "Month", "Year"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Columns"),
#"Grouped Rows" = Table.Group(#"Removed Duplicates", {"MOnthYear"}, {{"DistinctCount Customer", each Table.RowCount(Table.Distinct(_)), Int64.Type}})
in
#"Grouped Rows"

 

 

you can apply a sort order to the month year column as well.

 

Appreciate a thumbs up if this helps.

 

Please accept this as a solution if your question is resolved.

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.