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

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

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
Solution Supplier
Solution Supplier

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
Solution Supplier
Solution Supplier

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
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

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

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.