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
DJKarma
Frequent Visitor

Get Count in Query Editor by 2 Columns

I have to create such a column in PowerBI Query Editor

 

This is the base table --

Customer IDUnit IDDate Stamped
1XX1st jan1
1YY1st jan2
1XX2nd jan2
1YY2nd jan3
1XX3rd jan2
2CC3rd jan1
2AA5th jan1
2BB5th jan1

 


The logic is for Each Customer ID Per date ,it should give total of Stamped infront of  every Unit ID.

Output should look like this --

Customer IDUnit IDDateFinal Stamp CountReason ( don’t need this column)
1XX1st jan3( Row 1st + Row 2nd )
1YY1st jan3( Row 1st + Row 2nd )
1XX2nd jan5( Row 3rd+ Row 4th )
1YY2nd jan5( Row 3rd+ Row 4th )
1XX3rd jan2( Row 5th Only )
2CC3rd jan1( Row 6th Only )
2AA5th jan2( Row 7th + Row 8th )
2BB5th jan2( Row 7th + Row 8th )

 

So as you can see for Customer ID 1, both XX and YY will have total of there stamped infront of them (also based on date), and likewise for all other rows.
I have tried various combinations of groupby but i am unable to pinpoint this. Can you please help me out ?

PS  I NEED THIS IN QUERY EDITOR
@tamerj1 , last time you provided logic on point, can you help me out with this one ?

1 ACCEPTED SOLUTION

Hi @DJKarma ,

Please have a try.

Create a measure.

Measure = calculate(SUM('Table'[STAMPED]),FILTER(ALL('Table'),'Table'[DATE]=SELECTEDVALUE('Table'[DATE])&&'Table'[customer is]=SELECTEDVALUE('Table'[customer is])))

Or a column.

Measure = calculate(SUM('Table'[STAMPED]),FILTER(('Table'),'Table'[DATE]=EARLIER('Table'[DATE])&&'Table'[customer is]=EARLIER('Table'[customer is])))

vpollymsft_0-1665123725713.png

Best Regards

Community Support Team _ Polly

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

8 REPLIES 8
DJKarma
Frequent Visitor

I have 91K rows , I need to process this on. Its still not done processing, its taking forever, despite filtering only to few entries . I appreciate your help, but is thr any alternative? I can do with a measure as well.

Hi @DJKarma ,

Please have a try.

Create a measure.

Measure = calculate(SUM('Table'[STAMPED]),FILTER(ALL('Table'),'Table'[DATE]=SELECTEDVALUE('Table'[DATE])&&'Table'[customer is]=SELECTEDVALUE('Table'[customer is])))

Or a column.

Measure = calculate(SUM('Table'[STAMPED]),FILTER(('Table'),'Table'[DATE]=EARLIER('Table'[DATE])&&'Table'[customer is]=EARLIER('Table'[customer is])))

vpollymsft_0-1665123725713.png

Best Regards

Community Support Team _ Polly

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

The Column worked perfectly. Thank you so much bro

v-rongtiep-msft
Community Support
Community Support

Hi @DJKarma ,

I have created a simple sample, please refer to it to see if it helps you.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUYqIABJGBkZG+ob6IL6hUqwORCYyElXGCC6DrMcIRQZZD0jGGKseY7gekBpnZ1QZQ7iMoyNCxhRFxskJQyYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"customer is" = _t, #"unit id" = _t, DATE = _t, STAMPED = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"customer is", Int64.Type}, {"unit id", type text}, {"DATE", type date}, {"STAMPED", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([#"DATE"])),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "month", each Date.Month([#"DATE"])),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "day", each Date.Day([#"DATE"])),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Custom", each List.Sum(Table.SelectRows(#"Added Custom2", (x)=>x[Year]=[Year] and x[month]=[month] and x[day]=[day] and x[customer is]=[customer is])[STAMPED]))
in
    #"Added Custom3"

vpollymsft_0-1665110995895.png

If I have misunderstood your meaning, please provide more details with your desired output and pbix file without privacy information .

 

Best Regards

Community Support Team _ Polly

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

I need to condition it according to my data, can you please give me the last formula to put it in here.
Assuming i have year, month,day columns already extracted from date.

 

That is when i goto and click add custom column. What should i put it in the box 
Custom =

???

 

This formulae is based on custom2, but i am trying to click and add custom column.

#"Added Custom3" = Table.AddColumn(#"Added Custom2", "Custom", each List.Sum(Table.SelectRows(#"Added Custom2", (x)=>x[Year]=[Year] and x[month]=[month] and x[day]=[day] and x[customer is]=[customer is])[STAMPED]))

 

 

Hi @DJKarma ,

How about this?

List.Sum(Table.SelectRows(#"Added Custom2", (x)=>x[Year]=[Year] and x[month]=[month] and x[day]=[day] and x[customer is]=[customer is])[STAMPED])

vpollymsft_0-1665121273661.png

Best Regards

Community Support Team _ Polly

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks, for editing, as i am learning.

List.Sum(Table.SelectRows(#"Added Custom2", 

 

When i put this query , it says Custom2 was not recognized, I am assuming I did extract columns from date and renamed to look exactly like yours. And my last instance was Renamed Columns. So shall i put it as--

 

List.Sum(Table.SelectRows(#"Renamed Columns", (x)=>x[Year]=[Year] and x[month]=[month] and x[day]=[day] and x[customer is]=[customer is])[STAMPED])

?

Hi @DJKarma ,

You are right. The red mark in the formula represents the previous step of the present step.

vpollymsft_0-1665121995881.png

 

Best Regards

Community Support Team _ Polly

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.