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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
elia22gr
Helper III
Helper III

Dynamically change the referenced column in a DAX Measure or in Merging Queries

Hi team,

 

I have the below case. I have specific targets (Target Name 1, Target Name 2 etc) which each month have changes in criterias. For example on January the Target Name 1 has a single criteria bases on Item Group column of Dim_Products where eligible are only the products of Item Group Name 1, the Target Name 2 has 3 Criterias, the eligible products are those that are in Category Name 1 (Dim Products coulumn Category), and Subcategory Name 1  (Dim Products coulumn Subcategory) and Brand Name 1 (Dim Products coulumn Brand), the Target Name 3 has 1 criteria the products that have Subcategory Name 2 (Dim Products coulumn Subcategory). On February the same targets have different criterias. Target Name 1 has 1 criteria, Products that have Category Name 2 (Dim Products coulumn Category), Target Name 2 has 2 criterias, products that have Item Group Name 2 (Dim Products coulumn Item Group) and Subcategory Name 4 (Dim Products coulumn Subcategory), andTarget Name 3 has 1 criteria Products with Brand Name 17 (Dim Products coulumn Brand).

 

Targets Settings Table

MonthTargetCriteria_1Value_1Criteria_2Value_2Criteria_3Value_3
JanuaryTarget Name 1Item GroupItem Group Name 1TTLTTLTTLTTL
JanuaryTarget Name 2CategoryCategory Name 1SubcategorySubcategory Name 1BrandBrand Name 1
JanuaryTarget Name 3SubcategorySubcategory Name 2TTLTTLTTLTTL
FebruaryTarget Name 1CategoryCategory Name 2TTLTTLTTLTTL
FebruaryTarget Name 2Item GroupItem Group Name 2SubcategorySubcategory Name 4TTLTTL
FebruaryTarget Name 3BrandBrand Name 17TTLTTLTTLTTL

In Criterias Columns always refering a column from Dim Products Table and on Valuses columns a value from this column

 

Dim Products Table

ProductItem GroupCategorySubcategoryBrand
Product 1Item Group Name 1Category Name 1Subcategory Name 1Brand Name 1
Product 2Item Group Name 2Category Name 3Subcategory Name 4Brand Name 4
Product 3Item Group Name 3Category Name 5Subcategory Name 8Brand Name 9
Product 4Item Group Name 3Category Name 6Subcategory Name 9Brand Name 11
Product 5Item Group Name 1Category Name 2Subcategory Name 3Brand Name 3
Product 6Item Group Name 4Category Name 8Subcategory Name 11Brand Name 13
Product 7Item Group Name 3Category Name 5Subcategory Name 7Brand Name 8
Product 8Item Group Name 5Category Name 10Subcategory Name 14Brand Name 16
Product 9Item Group Name 1Category Name 1Subcategory Name 1Brand Name 1
Product 10Item Group Name 5Category Name 11Subcategory Name 15Brand Name 19
Product 11Item Group Name 3Category Name 7Subcategory Name 10Brand Name 12
Product 12Item Group Name 5Category Name 12Subcategory Name 16Brand Name 20
Product 13Item Group Name 1Category Name 1Subcategory Name 2Brand Name 2
Product 14Item Group Name 4Category Name 9Subcategory Name 13Brand Name 15
Product 15Item Group Name 3Category Name 5Subcategory Name 8Brand Name 10
Product 16Item Group Name 5Category Name 11Subcategory Name 15Brand Name 18
Product 17Item Group Name 2Category Name 3Subcategory Name 4Brand Name 5
Product 18Item Group Name 5Category Name 10Subcategory Name 14Brand Name 17
Product 19Item Group Name 3Category Name 6Subcategory Name 9Brand Name 11
Product 20Item Group Name 4Category Name 8Subcategory Name 12Brand Name 14
Product 21Item Group Name 2Category Name 4Subcategory Name 5Brand Name 6
Product 22Item Group Name 2Category Name 4Subcategory Name 6Brand Name 7

How can I manage it to filter the sales (fact Table) as it is set up by target? Actual vs Target Name 1, Actuals vs Target Name 2 etc?

Notice that each month each target may change the criteria (so as the column needs to be filtered out of the Dim Products table).

 

I need either an approch for Power Query or DAX Measure.

 

Thank you in advance,

Eleftheria Koniari

2 REPLIES 2
johnt75
Super User
Super User

You can use Power Query to to transform the criteria table into a list of matching products, e.g.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8krMK00sqlTSUQpJLEpPLVHwS8xNVTAE8j1LUnMV3IvySwtQOAgFISE+WMhYHVyGGgH5zoklqen5YCkYE2FgcGlSMkIeiYdQ4lSUmJcCo2HCuK00JmyqEV6fuKUmFeEIH5xeIctEI0IhbkTYKybEWmaMIyTNcbs8FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, Target = _t, Criteria_1 = _t, Value_1 = _t, Criteria_2 = _t, Value_2 = _t, Criteria_3 = _t, Value_3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Month", type text}, {"Target", type text}, {"Criteria_1", type text}, {"Value_1", type text}, {"Criteria_2", type text}, {"Value_2", type text}, {"Criteria_3", type text}, {"Value_3", type text}}),
    Prods = List.Buffer( Table.ToRecords( Products)),
    AddedList = Table.AddColumn( #"Changed Type", "Prod Names", each
        let
            Criteria1Col = [Criteria_1],
            Criteria1Val = [Value_1],
            Criteria2Col = [Criteria_2],
            Criteria2Val = [Value_2],
            Criteria3Col = [Criteria_3],
            Criteria3Val = [Value_3],
            MatchingProds = List.Select( Prods, each 
                ( Criteria1Col = "TTL" or Record.Field( _, Criteria1Col) = Criteria1Val) and 
                ( Criteria2Col = "TTL" or Record.Field( _, Criteria2Col) = Criteria2Val) and 
                ( Criteria3Col = "TTL" or Record.Field( _, Criteria3Col) = Criteria3Val)
            )
        in 
            List.Transform( MatchingProds, each [Product])
    , type list),
    #"Removed Columns" = Table.RemoveColumns(AddedList,{"Criteria_1", "Value_1", "Criteria_2", "Value_2", "Criteria_3", "Value_3"}),
    #"Expanded Prod Names" = Table.ExpandListColumn(#"Removed Columns", "Prod Names"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Prod Names",{{"Prod Names", type text}})
in
    #"Changed Type1"

You could then use TREATAS to match the products for a given target / month to the products table, e.g.

Sales for Target 1 =
VAR MatchingProducts =
    CALCULATETABLE (
        VALUES ( Targets[Product] ),
        TREATAS ( VALUES ( 'Date'[Month] ), Targets[Month] ),
        Targets[Target] = "Target 1"
    )
VAR Result =
    CALCULATE (
        [Sales Amount],
        KEEPFILTERS ( TREATAS ( MatchingProducts, Products[Product] ) )
    )
RETURN
    Result

Hi @elia22gr,

I would also take a moment to thank @johnt75 , for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.
 

I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If the response has addressed your query, please Accept it as a solution so that other community members can find it easily.

 

Thank you,

Harshitha.

Community Support Team.

 

 

Helpful resources

Announcements
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.