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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

Count number of columns measure

Hi, 

 

is there any way to count the number of columns in a table? (to be displayed in a visual) 

I don't see a function that would do this as a measure. 

My other idea would be to transpose the table and count rows, but I don't see a function for that either.

 

Thanks,

Ruben

1 ACCEPTED SOLUTION
jdbuchanan71
Super User
Super User

You can do it in PowerQuery.  If you reference your first query as the source you can keep only the first row then unpivot all the columns.  In my example I have a table called "data"

let
    Source = data,
    #"Kept First Rows" = Table.FirstN(Source,1),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Kept First Rows", {}, "Attribute", "Value")
in
    #"Unpivoted Columns"

Just make sure to select all the columns before you unpivot.

TableCountColumns.jpg

Then you feed this to the model and do the count off the new table.  You could even remove the value column before loading it.

 

 

 

View solution in original post

4 REPLIES 4
Icey
Community Support
Community Support

Hi @Anonymous ,

 

Try this:

1. In Power Query Editor, Duplicate your table.

countofcolumns.png

2. Select all columns and UnPivot.

countofcolumns2-1.png

countofcolumns2.png

3. Remove 'Value' column.

countofcolumns3.png

4. Remove duplicate rows.

countofcolumns4.png.jpg

5. Create a measure.

Count of Columns = COUNTROWS ( 'Table (2)' )

countofcolumns5.png

6. Hidden 'Table (2)'.

countofcolumns6.png

 

Best Regards,

Icey

 

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

jdbuchanan71
Super User
Super User

You can do it in PowerQuery.  If you reference your first query as the source you can keep only the first row then unpivot all the columns.  In my example I have a table called "data"

let
    Source = data,
    #"Kept First Rows" = Table.FirstN(Source,1),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Kept First Rows", {}, "Attribute", "Value")
in
    #"Unpivoted Columns"

Just make sure to select all the columns before you unpivot.

TableCountColumns.jpg

Then you feed this to the model and do the count off the new table.  You could even remove the value column before loading it.

 

 

 

kentyler
Solution Sage
Solution Sage

You and use the COUNTROWS() function to count rows once you transpose your table





Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


Anonymous
Not applicable

@kentyler 

How do I transpose the table?

 

Kindest regards,

Ruben

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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