Forum Discussion

MP_123's avatar
MP_123
Microsoft Employee
9 years ago
Solved

search between text

hi

My table looks like this:

 

Product                               Country           Price

Milk;Chocolate;Jam            IN;US;GB             10

Milk;Chocolate                    US:AU                  5 

 

the meaning here: the Price of Milk in IN is 10, The Price of Chocolate in AU is 5.

there is no Milk in GB, and there is no Chocolate in IN and GB.

(the idea here is the places, first place of product & first place of Country)

i have two slicers, with the single values: Milk, Chocolate, Jam. and Contries: IN, US, GB,AU

the issue: if i select on the slicers Milk & IN, the measure will be 10.

if i select Chocolate & GB i will get blank.

 

is someone have an idea???

thanks a lot!

  • Anonymous's avatar
    Anonymous
    9 years ago

    Those steps work for me, though you'll have to adapt with your real data, and it won't scale well to large numbers of rows -  you would need to repeat the "Split by ;" once for each data row:

    • Transpose to line up the Product, Country and Price as rows
    • Split each non-Price column by ";" (and PQ adds the Changed Type steps)
    • Transpose back to Product, Country, Price order
    • Fill down the Price to now-empty rows
    • Rename columns back to Product, Country, Price

     

    Alternately, you can try something with Table.From Records - e.g. http://stackoverflow.com/questions/31885049/power-query-transform-a-column-by-multiplying-by-another-column

     

     

  • Anonymous's avatar
    Anonymous
    9 years ago

    I'm not sure why it's duplicating but, with millions of rows, this method won't work as you need to split for each row (after it's transposed).  Another method came to mind, BUT it's dependent on the maximum number of Products/Countries per line - anything more than 3-7 per line will be unworkable, and it may be slow for millions of lines - can you fix this at the source.  

     

    For what it's worth, for a maximum of three product/country per line, add these lines instead to your Power Query:

        #"Split Product by Delimiter" = Table.SplitColumn(#"Renamed Columns","Product",Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv),{"Product.1", "Product.2", "Product.3"}),
        #"Split Country by Delimiter" = Table.SplitColumn(#"Split Product by Delimiter","Country",Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv),{"Country.1", "Country.2", "Country.3"}),
        #"Renamed Price Column 1" = Table.RenameColumns(#"Split Country by Delimiter",{{"Price", "Price.1"}}),
        #"Duplicated Price Column 2" = Table.DuplicateColumn(#"Renamed Price Column 1", "Price.1", "Price.2"),
        #"Duplicated Price Column 3" = Table.DuplicateColumn(#"Duplicated Price Column 2", "Price.2", "Price.3")
    in
        #"Duplicated Price Column 3"

    Then REFERENCE that table THREE times to make three derived copies, once for each of (up to) three product/country per line.

     

    In the first copy, keep only columns Product.1, Country.1, Price.1, then rename all columns to drop the .x suffix

    In the second copy, keep only columns Product.2, Country.2, Price.2, then rename all columns to drop the .x suffix

    In the third copy, keep only columns Product.3, Country.3, Price.3, then rename all columns to drop the .x suffix

     

    Then APPEND all three tables to a New table (they should all have columns Product, Country, Price), and filter out those with "null" Product - these will be rows created for lines with < 3 entries per line.

     

    That only took a few minutes to create but, again, it may not scale to millions of records and if your data has too many variations then consider a function around Table.FromRecords etc. as a next option.

8 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    You could tidy the data with Power Query similar to below transposes and splitting etc. (assuming that's a ; and not a : in the secod Country row:

    Let
     Source = <Your original query step here>,
        #"Transposed Table" = Table.Transpose(Source),
    #"Split Column by Delimiter" = Table.SplitColumn(Table.TransformColumnTypes(#"Transposed Table", {{"Column1", type text}}, "en-AU"),"Column1",Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv),{"Column1.1", "Column1.2", "Column1.3"}), #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Column1.1", type text}, {"Column1.2", type text}, {"Column1.3", type text}}), #"Split Column by Delimiter1" = Table.SplitColumn(Table.TransformColumnTypes(#"Changed Type1", {{"Column2", type text}}, "en-AU"),"Column2",Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv),{"Column2.1", "Column2.2"}), #"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Column2.1", type text}, {"Column2.2", type text}}), #"Transposed Table1" = Table.Transpose(#"Changed Type2"), #"Filled Down" = Table.FillDown(#"Transposed Table1",{"Column3"}), #"Renamed Columns1" = Table.RenameColumns(#"Filled Down",{{"Column1", "Product"}, {"Column2", "Country"}, {"Column3", "Price"}}) in #"Renamed Columns1"

    That will give you a more structured table on which to query.  You may also want to split Country and Product out to lookup (Dimension) tables to feed your slicers.

     

    • MP_123's avatar
      MP_123
      Microsoft Employee

      Anonymous

       

      thank you very much! but i don't understand how the splitting wil help me.

      i need to connect between product and country within their order: first with first, second with second

       

      thanks!

      • Anonymous's avatar
        Anonymous
        Not applicable

        Those steps work for me, though you'll have to adapt with your real data, and it won't scale well to large numbers of rows -  you would need to repeat the "Split by ;" once for each data row:

        • Transpose to line up the Product, Country and Price as rows
        • Split each non-Price column by ";" (and PQ adds the Changed Type steps)
        • Transpose back to Product, Country, Price order
        • Fill down the Price to now-empty rows
        • Rename columns back to Product, Country, Price

         

        Alternately, you can try something with Table.From Records - e.g. http://stackoverflow.com/questions/31885049/power-query-transform-a-column-by-multiplying-by-another-column