Forum Discussion

cmleo's avatar
cmleo
Regular Visitor
1 year ago
Solved

Look up data based on multiple conditions

I need to be able to find values in other tables based on conditions from two different tables:

 

I have the follwing tables: 

- Table 1 with information about countries and Order no.

- Table 2 with list of Order no. and groceries

- Table 3 Warehouse 1: with countries, groseries and if it is on stock or not

- Table 4 WH 2: with countries, groseries and if it is on stock or not

- Table 5 WH 3: with countries, groseries and if it is on stock or not

Table 3, 4 and 5 is direct query from another semantic model, hence cannot be appended.

 

I need to make a table that can show the combination of countri and groceries for an oder (chosen by slicer) and if these are on stock or not in any of the warehouses.

 

I have a relationsship between table 1 and 2. Where 2 is filtering 1. 

 

How can I fetch the status from the 3 direct query tables?

 

 

 

  • Hi. 
    Thank you for your advise. 

    First I am reaching out to the once who manage the Power BI semantic model, and asking them to append the tables.

    Hopefully they will do it, as it will be very time consuming and maintenance heavy to make measures for each grocery.

     

    But thank you a lot.

11 Replies

  • Please include, in a usable format, not an image, a small set of rows for each of the tables involved in your request and show the data model in a picture, so that we can import the tables in Power BI and reproduce the data model. The subset of rows you provide, even is just a subset of the original tables, must cover your issue or question completely. Do not include sensitive information and do not include anything that is unrelated to the issue or question. Please show the expected outcome based on the sample data you provided and make sure, in case you show a Power BI visual, to clarify the columns used in the grouping sections of the visual.

     

    Need help uploading data? click here

     

    Want faster answers? click here

    • cmleo's avatar
      cmleo
      Regular Visitor

      Sure. I hust wasn't aware of how to paste in the data correctly.

      Table 1

      CountryOrder no
      United States1
      Canada2
      Mexico3
      Brazil4
      Argentina1
      United Kingdom2
      Germany3
      France4
      Italy1
      Spain2
      China3
      Japan4
      South Korea1
      India2
      Australia3
      New Zealand4
      South Africa1
      Egypt2
      Russia3
      Saudi Arabia4

       

      Table 2

      GroceryOrder no
      Apples1
      Bananas1
      Carrots1
      Potatoes1
      Tomatoes2
      Onions2
      Milk2
      Eggs2
      Cheese3
      Bread3
      Rice3
      Pasta4
      Chicken4
      Beef4
      Fish4
      Butter5
      Yogurt5
      Cereal5
      Flour6
      Sugar6

       

      Table 3

      CountryGroceryStatus
      United StatesApplesOn stock
      CanadaBananasNot on stock 
      MexicoCarrotsNot on stock 
      BrazilPotatoesOn stock
      ArgentinaTomatoesNot on stock 
      United KingdomOnionsOn stock
      GermanyMilkOn stock
      FranceEggsOn stock
      ItalyCheeseOn stock
      SpainBreadOn stock
      ChinaRiceOn stock
      JapanPastaOn stock
      South KoreaChickenOn stock
      IndiaBeefNot on stock 
      AustraliaFishNot on stock 
      New ZealandButterNot on stock 
      South AfricaYogurtNot on stock 
      EgyptCerealNot on stock 
      RussiaFlourOn stock
      Saudi ArabiaSugarOn stock

       

      Table 4

      CountryGroceryStatus
      United StatesApplesNot on stock 
      CanadaBananasOn stock
      MexicoCarrotsOn stock
      BrazilPotatoesOn stock
      ArgentinaTomatoesOn stock
      United KingdomOnionsOn stock
      GermanyMilkOn stock
      FranceEggsNot on stock 
      ItalyCheeseNot on stock 
      SpainBreadNot on stock 
      ChinaRiceNot on stock 
      JapanPastaNot on stock 
      South KoreaChickenNot on stock 
      IndiaBeefOn stock
      AustraliaFishOn stock
      New ZealandButterOn stock
      South AfricaYogurtOn stock
      EgyptCerealOn stock
      RussiaFlourNot on stock 
      Saudi ArabiaSugarNot on stock 

       

      Table 5:

      CountryGroceryStatus
      United StatesApplesOn stock
      CanadaBananasOn stock
      MexicoCarrotsOn stock
      BrazilPotatoesOn stock
      ArgentinaTomatoesOn stock
      United KingdomOnionsOn stock
      GermanyMilkOn stock
      FranceEggsOn stock
      ItalyCheeseOn stock
      SpainBreadNot on stock 
      ChinaRiceNot on stock 
      JapanPastaNot on stock 
      South KoreaChickenNot on stock 
      IndiaBeefNot on stock 
      AustraliaFishNot on stock 
      New ZealandButterNot on stock 
      South AfricaYogurtNot on stock 
      EgyptCerealOn stock
      RussiaFlourNot on stock 
      Saudi ArabiaSugarNot on stock 
    • cmleo's avatar
      cmleo
      Regular Visitor

      My datamodel looks like this: 

      I have tried various ways of relatioships, but it does not show the desired output.

       

  • v-echaithra's avatar
    v-echaithra
    Community Support

    Hi cmleo ,

    You cannot append Table3, Table4, Table5 because they are Direct Query from semantic models. Also, there's no relationship between them and the rest of the model.
    Tables 3/4/5 are in DirectQuery mode and from separate semantic models, you cannot create relationships between them and your other (import) tables.

    This means no natural filter context flows from your selection of country or grocery.

    Any attempt to use visuals or relationships alone will not fetch data from those warehouse tables correctly.

    Combine the data from Table1 and Table2 based on Order no, Look up stock status from each of the warehouse tables (Table3, Table4, Table5).Show "On stock" if any warehouse has the item in stock.

    Create a new table to show all Country-Grocery combinations for the selected Order no:

    CountryGroceryMatrix =
    VAR SelectedOrderNo = SELECTEDVALUE('Table1'[Order no])
    RETURN
    FILTER (CROSSJOIN (
    VALUES('Table1'[Country]),
    VALUES('Table2'[Grocery])),
    RELATED('Table1'[Order no]) = SelectedOrderNo &&
    RELATED('Table2'[Order no]) = SelectedOrderNo)

    Now create a DAX measure to determine if the item is on stock in any warehouse:


    Stock Status =
    VAR _country = SELECTEDVALUE('CountryGroceryMatrix'[Country])
    VAR _grocery = SELECTEDVALUE('CountryGroceryMatrix'[Grocery])


    VAR WH1 = TRIM(LOWER(CALCULATE(
    MAX('Table3'[Status]),
    FILTER('Table3', 'Table3'[Country] = _country && 'Table3'[Grocery] = _grocery)
    )))
    VAR WH2 = TRIM(LOWER(CALCULATE(
    MAX('Table4'[Status]),
    FILTER('Table4', 'Table4'[Country] = _country && 'Table4'[Grocery] = _grocery)
    )))
    VAR WH3 = TRIM(LOWER(CALCULATE(
    MAX('Table5'[Status]),
    FILTER('Table5', 'Table5'[Country] = _country && 'Table5'[Grocery] = _grocery)
    )))

    VAR StatusList = { WH1, WH2, WH3 }

    RETURN
    IF (
    "not on stock" IN StatusList,
    "Not on Stock",
    IF (
    "on stock" IN StatusList,
    "On Stock",
    "Null"
    ))

     


    Hope this helps,
    Thank you.

  • v-echaithra's avatar
    v-echaithra
    Community Support

    Hi cmleo ,

    We’d like to follow up regarding the recent concern. Kindly confirm whether the issue has been resolved, or if further assistance is still required. We are available to support you and are committed to helping you reach a resolution.

    Best Regards,
    Chaithra E.

  • cmleo's avatar
    cmleo
    Regular Visitor

    Hi. 

    Thank you so much for looking into my issue. 
    I acutally managed to solve it in a bit different way than suggested here, but I still have an issue. 

    I now have a calculated table that refers to remote table. This results in refresh faillure when published. How can I get about that? 

     

    I combined the columns in a new table this way: 

    WH status table = UNION(
        SELECTCOLUMNS('Warehouse 3 table', "Groseries", 'Warehouse 3 table'[Groseries], "Country", 'Warehouse 3 table'[Country], "Status", 'Warehouse 3 table'[Status]),
        SELECTCOLUMNS('Warehouse 4 table', "Groseries", 'Warehouse 4 table'[Groseries], "Country", 'Warehouse 4 table'[Country], "Status", 'Warehouse 4 table'[Status]),
        SELECTCOLUMNS('Warehouse 5 table', "Groseries", 'Warehouse 5 table'[Groseries], "Country", 'Warehouse 5 table'[Country], "Status", 'Warehouse 5 table'[Status]))

    From there I just made a simple relationship to Table 2. With Groceries in Table 2 filtering Groceries in the new WH Status table.
    • v-echaithra's avatar
      v-echaithra
      Community Support

      Hi cmleo ,

      In DirectQuery mode, calculated tables are not supported because they require in-memory processing, which isn't compatible with the real-time querying approach of DirectQuery. Instead of loading the full data model into memory, DirectQuery sends queries directly to the underlying data source only when required by visuals.

       

      Alternatively, Use DAX Measures for dynamic calculations, consider using measures instead of calculated tables. Measures compute results based on the current report context without storing additional data. Transform Data in Power Query if you need to filter or reshape data from remote tables, use Power Query to perform these transformations before loading the data into your report.
      Create Views in the Source System, if complex transformations are needed, consider building a view in the underlying data source to encapsulate the logic. You can then connect Power BI directly to this view.
      Make sure you have the necessary permissions to access the shared semantic model and any underlying data sources. Check Data Source Settings: In the Power BI Service, ensure your data source settings and credentials are set up correctly for the shared semantic model.


      Hope this helps,
      Chaithra E.

      • cmleo's avatar
        cmleo
        Regular Visitor

        Hi 

         

        I really appreciate your input. 
        My direct query tables comes from other Power BI semantic models. They are therefore, not visible in power wuery, and I cannot have calculated tabels. So I am limited to DAX measures, which I untill now hasn't had any success with.

        So if you have good ideas to how I can convert my calculated column into a measure, I will be thrilled. 🙂 

  • v-echaithra's avatar
    v-echaithra
    Community Support

    Hi cmleo ,

    Thank you Irwan  for your inputs.

    May I ask if you have resolved this issue? Please let us know if you have any further issues, we are happy to help.

    Thank you.