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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
tomekm
Helper III
Helper III

DAX count non-blank columns in a range of columns

Hello,

 

I'm trying to create a calculated column that will count the number of non-blank/non-zero columns in a range of columns (price 1, price 2, price 3, price 4). My table looks like the below and the end result is in the last column:

 

projectprice1price2price3price4count of non-blank columns
a25  2
b345 3
c679124

 

 

Thank you.

2 ACCEPTED SOLUTIONS
muhammad_786_1
Super User
Super User

Hy @tomekm 

 

You can use the @lbendlin  solution or, if you prefer, in the calculated column, use this measure.

 

Count_Non_Blank = 
VAR CountValues = 
    COUNTX(
        { [price1], [price2], [price3], [price4] }, 
        IF(NOT(ISBLANK([Value])) && [Value] <> 0, 1, BLANK())
    )
RETURN 
    CountValues

 

muhammad_786_1_0-1743457968947.png

 

Best Regards,
Muhammad Yousaf

 

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

 

LinkedIn

View solution in original post

@muhammad_786_1 

lbendlin_0-1743458423490.png

of course this only works for four hard coded columns that have a number type.

 

View solution in original post

8 REPLIES 8
muhammad_786_1
Super User
Super User

Hy @tomekm 

 

You can use the @lbendlin  solution or, if you prefer, in the calculated column, use this measure.

 

Count_Non_Blank = 
VAR CountValues = 
    COUNTX(
        { [price1], [price2], [price3], [price4] }, 
        IF(NOT(ISBLANK([Value])) && [Value] <> 0, 1, BLANK())
    )
RETURN 
    CountValues

 

muhammad_786_1_0-1743457968947.png

 

Best Regards,
Muhammad Yousaf

 

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

 

LinkedIn

Thank you!

@muhammad_786_1 

lbendlin_0-1743458423490.png

of course this only works for four hard coded columns that have a number type.

 

Yes, you're right! This works well only for hardcoded columns with a number type. Appreciate your insights as always!

Thank you!

lbendlin
Super User
Super User

Unpivot and group

 

lbendlin_0-1743457102112.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTICYlMgVgDjWJ1opSQgyxiITeAyINFkIMsMiM2B2BKIDY2UYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [project = _t, price1 = _t, price2 = _t, price3 = _t, price4 = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"project"}, "Attribute", "Value"),
    #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Value", Int64.Type}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Value] <> null)),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"project"}, {{"Count of non-blank columns", each Table.RowCount(_), Int64.Type}})
in
    #"Grouped Rows"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the entire Source step with your own source.

Thank you but I need to create a calculated column instead of making changes in power query. Would you have any suggetions for that?

 

Cheers.

You cannot do that - Power BI does not allow arbitrary numbers of columns. At a minimum you need to unpivot.

 

A calculated column is calculated for each row of your data.  After unpivoting that row has a different meaning, it is no longer one row per project.

 

lbendlin_0-1743458023526.png

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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