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

Add a conditional count column

Hi, I am trying to add a new column to my query that will give me for each row, the count of transactions that occurred for an account for a specific year. In my example account A had two transactions in 2018, so for each Account A row created in 2018 I expect to see 2, all the rest is a one.

 

IdAccount IdCreateDateNewColumn
1A20191
2A20182
3A20182
4B20191
5B20181

 

Thanks a lot!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

This should be a fast solution for large tables.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYiMDQ0ulWJ1oJSOEgAVYwBhdwATIcULWYooQAKqIBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Id = _t, #"Account Id" = _t, CreateDate = _t]),
    #"Grouped Rows" = Table.Group(Source, {"Account Id", "CreateDate"}, {{"Count", each Table.RowCount(_), type number}}),
    #"Merged Queries" = Table.NestedJoin(Source, {"Account Id", "CreateDate"}, #"Grouped Rows", {"Account Id", "CreateDate"}, "Grouped Rows", JoinKind.Inner),
    #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Count"}, {"NewColumn"})
in
    #"Expanded Grouped Rows"

 

View solution in original post

4 REPLIES 4
Jimmy801
Community Champion
Community Champion

Hello @ousslaraichi 

If these answers to your requested helped or solved your problem, please mark them as solution.
Kudos are nice to - thanks
Have fun

Jimmy

Anonymous
Not applicable

This should be a fast solution for large tables.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYiMDQ0ulWJ1oJSOEgAVYwBhdwATIcULWYooQAKqIBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Id = _t, #"Account Id" = _t, CreateDate = _t]),
    #"Grouped Rows" = Table.Group(Source, {"Account Id", "CreateDate"}, {{"Count", each Table.RowCount(_), type number}}),
    #"Merged Queries" = Table.NestedJoin(Source, {"Account Id", "CreateDate"}, #"Grouped Rows", {"Account Id", "CreateDate"}, "Grouped Rows", JoinKind.Inner),
    #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Count"}, {"NewColumn"})
in
    #"Expanded Grouped Rows"

 

Hey

 

Great solution @Anonymous 

 

Jimmy

Jimmy801
Community Champion
Community Champion

Hello

 

see this code example. Used RowCount and SelectRows to make the calculation

let
    Quelle = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYiMDQ0ulWJ1oJSOEgAVYwBhdwATIcULWYooQAKqIBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Id = _t, #"Account Id" = _t, CreateDate = _t]),
    AddColumn = Table.AddColumn
        (
            Quelle,
            "Count", 
            (add)=> Table.RowCount
                (
                    Table.SelectRows
                        (Quelle,
                        each [CreateDate]= add[CreateDate] and [Account Id]= add[Account Id]
                    )
                )
        )
in
    AddColumn

 

have fun

 

Jimmy

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.

Top Solution Authors
Top Kudoed Authors