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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
Mendes094
New Member

Distinct Count Colunm in Power Query

Hello, 

 

I need to make a distinct count column in POWER QUERY, example:

 

I have this colunm 'COD Filial' in Power Query:

 

Mendes094_0-1725463168352.png

 

the expected result would be colunm 'Distinct Count'

Mendes094_1-1725463339789.png

 Anybody can help me, please.

 

Tks.

 

3 ACCEPTED SOLUTIONS
Omid_Motamedise
Super User
Super User

use this code to reach the result presneted in the next image

let
Source = Table.FromColumns({{967,892,892,901,901,901}},{"COD FILIAL"}),
#"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each List.Count(List.Distinct(Table.FirstN(#"Added Index",[Index])[COD FILIAL])))
in
#"Added Custom"

Omid_Motamedise_0-1725488205980.png

 



View solution in original post

dufoq3
Super User
Super User

Hi @Mendes094, another solution:

 

Output:

dufoq3_0-1725554912508.png

let
    Source = Table.FromList({967,892,892,901,901,901}, (x)=> {x}, type table[COD FILIAL=Int64.Type]),
    GroupedRows = Table.Group(Source, {"COD FILIAL"}, {{"All", each _, type table},{"Count", each Table.RowCount(_), Int64.Type}}),
    RemovedOtherColumns = Table.SelectColumns(GroupedRows,{"All", "Count"}),
    ExpandedAll = Table.ExpandTableColumn(RemovedOtherColumns, "All", Table.ColumnNames(Source))
in
    ExpandedAll

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

v-xinruzhu-msft
Community Support
Community Support

Hi,

Thanks for the solutions everyone offered, and i want to offer some more information for user to refer to.

hello @Mendes094 

You can add an index column first.

vxinruzhumsft_0-1726022796792.png

Then add a custom column.

=let a=[Index]
in List.Count(List.Distinct(Table.SelectRows(#"Added Index",each  [Index]<=a)[COD FILIAL]))

Output

vxinruzhumsft_1-1726022839931.png

Best Regards!

Yolo Zhu

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

12 REPLIES 12
v-xinruzhu-msft
Community Support
Community Support

Hi,

Thanks for the solutions everyone offered, and i want to offer some more information for user to refer to.

hello @Mendes094 

You can add an index column first.

vxinruzhumsft_0-1726022796792.png

Then add a custom column.

=let a=[Index]
in List.Count(List.Distinct(Table.SelectRows(#"Added Index",each  [Index]<=a)[COD FILIAL]))

Output

vxinruzhumsft_1-1726022839931.png

Best Regards!

Yolo Zhu

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

 

dufoq3
Super User
Super User

Hi @Mendes094, another solution:

 

Output:

dufoq3_0-1725554912508.png

let
    Source = Table.FromList({967,892,892,901,901,901}, (x)=> {x}, type table[COD FILIAL=Int64.Type]),
    GroupedRows = Table.Group(Source, {"COD FILIAL"}, {{"All", each _, type table},{"Count", each Table.RowCount(_), Int64.Type}}),
    RemovedOtherColumns = Table.SelectColumns(GroupedRows,{"All", "Count"}),
    ExpandedAll = Table.ExpandTableColumn(RemovedOtherColumns, "All", Table.ColumnNames(Source))
in
    ExpandedAll

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

I think you dont understand, in my example I just show to you 6 values in COD FILIAL, but I have more than 200000 values in this colunm, this example is just a part.

I need to do something like add a custom colunm and imput a query in M to count distinct how many times each values appears in colunm COD FILIAL.

 

Do you can help me? need more examples?

 

Thanks!

@dufoq3 

I think you don't understand 😉

Have you read note below my posts?


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Hello, here is Mendes094 this is my second account.

 

In this case I already have the table in Power Query, I just need a code to insert a custom colunm.

For example, this step:

Source = Source = Table.FromList({967,892,892,901,901,901}, (x)=> {x}, type table[COD FILIAL=Int64.Type]),

Is not necessary.

Think that I have a big data and the photo is just a little part of the base.

Do you can help me? 

 

@dufoq3 

That step was necessary for sample data. If you want to use my query on your data - read note below my posts.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Omid_Motamedise
Super User
Super User

use this code to reach the result presneted in the next image

let
Source = Table.FromColumns({{967,892,892,901,901,901}},{"COD FILIAL"}),
#"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each List.Count(List.Distinct(Table.FirstN(#"Added Index",[Index])[COD FILIAL])))
in
#"Added Custom"

Omid_Motamedise_0-1725488205980.png

 



Hello, here is Mendes094 this is my second account.

 

In this case I already have the table in Power Query, I just need a code to insert a custom colunm.

For example, this step:

Source = Table.FromColumns({{967,892,892,901,901,901}},{"COD FILIAL"}),

Is not necessary.

 

Do you can help me? 

@Omid_Motamedise 

So use this

Table.AddColumn(#"Added Index", "Custom", each List.Count(List.Distinct(Table.FirstN(#"Added Index",[Index])[COD FILIAL])))

AlienSx
Super User
Super User

let
    Source = #table(type table [COD FILIAL = Int64.Type], {{967}, {892}, {892}, {901}, {901}, {901}}),
    f = (lst) => List.Repeat({List.Count(lst)}, List.Count(lst)),
    group = Table.Group(Source, "COD FILIAL", {"x", (x) => f(x[COD FILIAL])}),
    xpand = Table.ExpandListColumn(group, "x")
in
    xpand
Gabry
Super User
Super User

With a line 
AddCountColumn = Table.AddColumn(Source, "Count", each List.Count(List.Select(Source[data], (x) => x = _[data])))

Gabry
Super User
Super User

Hello,

let
// Step 1: Import the data
Source = Table.FromRows({
{1},
{1},
{1},
{2},
{2},
{3},
{4},
{4},
{4},
{4}},
{"data"}
),

// Step 2: Group by 'data' and count the occurrences
GroupedTable = Table.Group(Source, {"data"}, {{"Count", each Table.RowCount(_), Int64.Type}}),

// Step 3: Merge the original table with the grouped table to add the 'Count' column
MergedTable = Table.NestedJoin(Source, {"data"}, GroupedTable, {"data"}, "NewColumn", JoinKind.LeftOuter),

// Step 4: Expand the merged column to get the count
ExpandedTable = Table.ExpandTableColumn(MergedTable, "NewColumn", {"Count"}, {"Count"})
in
ExpandedTable

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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