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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
tt211595
Frequent Visitor

Add a column with rowwise calculation

Hello! I'd like to use M code to add a new column to a query. For each row, I'd like the new column to show how many distinct values are in the row (across multiple columns).

 

For example, if I have this query with three columns:

Column1Column2Column3
ANULLNULL
BCD
EEF

 

I'd like to add NewColumn, which does a distinct count for each row in the query:

ID1ID2ID3NewColumn
AAA1
BCD3
EEF2

 

Thanks in advance!

1 ACCEPTED SOLUTION
kameronyork
Resolver I
Resolver I

This can be accomplished by creating a list within a function in Power Query. 

The following image shows the outcome you are looking for:
Table.png
This is the function I used to acheive this solution:
Function.png
This can be done in multiple steps or just one step as seen above.  The process is as follows:
1. Create a list using the Record.ToList function.  Inside brackets you will identify what columns will pass their values to the list.
2. Use the List.Distinct function to remove duplicate values from the list.  Nulls will remain, but the following step will ignore them.
3. Use the List.NonNullCount function to return the count of values in the list that are not Null.

I hope this helps!  If it has, please mark this reponse as the solution 🙂

View solution in original post

5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

This M code works as well

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Added Custom" = Table.AddColumn(Source, "Custom", each List.Count(List.Distinct(List.Select(Record.ToList(_), each _<>null))))
in
    #"Added Custom"

Hope this helps.

Ashish_Mathur_0-1691808960490.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
kameronyork
Resolver I
Resolver I

This can be accomplished by creating a list within a function in Power Query. 

The following image shows the outcome you are looking for:
Table.png
This is the function I used to acheive this solution:
Function.png
This can be done in multiple steps or just one step as seen above.  The process is as follows:
1. Create a list using the Record.ToList function.  Inside brackets you will identify what columns will pass their values to the list.
2. Use the List.Distinct function to remove duplicate values from the list.  Nulls will remain, but the following step will ignore them.
3. Use the List.NonNullCount function to return the count of values in the list that are not Null.

I hope this helps!  If it has, please mark this reponse as the solution 🙂

Thanks very much!

Ahmedx
Super User
Super User

plse try this

List.Count(
 List.Distinct(
List.Select(
Record.FieldValues(_),each _<> "" and _<> null)))

Screenshot_2.png

This worked, too. Thank you!

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon & SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.