DAX - select column based on row value in other column

vistrup

New Member
Joined
Jun 27, 2017
Messages
8
Hi,

I have 2 tables. A fact table (table 1) and a table (table 2) that one have one column which contains all the column names from the fact table.

table 1:
Product IDProduct NameCategory
12Bike 12 incBike
35NutNuts and bolts
40Bike
11Bolt

<tbody>
</tbody>


table 2:
Column name
Product ID
Product Name
Category

<tbody>
</tbody>


What I would like to do is as following. Creating a new column in table 2 that calculated the number of blank rows in table 1 for the column specified in the first column in table 2.

Table 2 after calc:
Column nameNumber of blanks
Product ID0
Product Name1
Category1

<tbody>
</tbody>


I can do it in Excel with the following formula: {=COUNTBLANK(INDEX(Table1;;MATCH([@[COLUMN NAME]];Table1[#Headers];0)))}

Any ideas on how to do so?

Thanks in advanced

Br
Vistrup
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi Gazpage,

Thanks for the link. However I cannot see how it should help me that I rename the columns in table 1?
 
Upvote 0
Ok, I typed in your data in table 1 manually and unpivotted using the following query (I called it 'Unpivot data')

Code:
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRS0lFyysxOVTA0UsjMS4bylGJ1opWMTYE8v9ISCFmskJiXopCUn1NSDJY1MQCKIys3NATxgPIg8dhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Product ID" = _t, #"Product Name" = _t, Category = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product ID", Int64.Type}, {"Product Name", type text}, {"Category", type text}}),
    #"Demoted Headers" = Table.DemoteHeaders(#"Changed Type"),
    #"Transposed Table" = Table.Transpose(#"Demoted Headers"),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Transposed Table", {"Column1"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Column1", "Column Name"}})
in
    #"Renamed Columns"

I then created a table with just the column names using.

Code:
let
    Source = #"Unpivot data",
    #"Removed Other Columns" = Table.SelectColumns(Source,{"Column Name"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Other Columns")
in
    #"Removed Duplicates"

Presumably you wouldn't need to do this ad you would already have the table.

I then deleted the relationship that was created automatically and added the following calculated column to this new table.

Code:
Number of Blanks = 
CALCULATE (
    COUNTROWS ('Unpivot data')+0,
    FILTER (
        'Unpivot data',
        'Unpivot data'[Column Name] = 
            'ColumnNames'[Column Name] &&
            'Unpivot data'[Value] = BLANK()
    )
)

Seems to do the trick.
 
Upvote 0
Hi again,

The only thing I can't get to working is the second line the first code (the source line). My guess is that this ensures that blanks are shown in the unpivotted tabel?
 
Upvote 0
Great. Blanks are shown in power bi but not excel :cry:

Furthermore I found out about the source line. it is how it is stated when using power query in power bi.
 
Upvote 0

Forum statistics

Threads
1,216,739
Messages
6,132,443
Members
449,728
Latest member
teodora bocarski

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top