Forum Discussion

david_flu's avatar
david_flu
Frequent Visitor
2 years ago
Solved

how can i write the Lookup functions please

Hello

 

Could i please get some help with the below tables please. I'm trying to add a column which shows the total sale amount for each product / each region / each week. is there a way to do it ? snip below for the product sales / product details table.

 

 

Many thanks in advance!

 

 

  • Hi david_flu 

    You did not mention how to calcualte the sales amount but I am assuming it is unit price * kg. If so, try this calc column in your first table:

    Sales =
    VAR __UNIT_PRICE =
        LOOKUPVALUE (
            'table2'[Unit Price],
            'table2'[Regions], 'table1'[Store],
            'table2'[Product], 'table1'[Regions]
        )
    RETURN
        __UNIT_PRICE * 'Table1'[KGs]
    

    If this isn't what you're looking for, please elaborate your use case and provide a sample data we can easily copy-paste (not an image) as well as your sample result.  Please refer to this sticky post when creating new posts in the future: https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/3332479#M1112153 

  • Daniel_PowerBI the solution danextian should work and you can also do this:

     

    add 2 new columns in each table and then set the relationship on these new columns, which will be one to many, one will be on the store/product side table. 

     

    //add new column in Sales Table
    Surrogate Key = SalesTable[Store] & SalesTable[Product]
    
    //add new column in Store Product table
    Surrogate Key = StoreProductTable[Store] & StoreProductTable[Product]
    
    //add new measure for sales
    Sales Measure = 
    SUMX (  SalesTable, SalesTable[Product Sales] & RELATED ( StoreProductTable[Unit Price] ) )

     

    and to visualize. use Week/Day/Store column from SalesTable and the measure Sales Measure

     

    Change the column names and table names as per your data model.

12 Replies

  • Hi david_flu 

    You did not mention how to calcualte the sales amount but I am assuming it is unit price * kg. If so, try this calc column in your first table:

    Sales =
    VAR __UNIT_PRICE =
        LOOKUPVALUE (
            'table2'[Unit Price],
            'table2'[Regions], 'table1'[Store],
            'table2'[Product], 'table1'[Regions]
        )
    RETURN
        __UNIT_PRICE * 'Table1'[KGs]
    

    If this isn't what you're looking for, please elaborate your use case and provide a sample data we can easily copy-paste (not an image) as well as your sample result.  Please refer to this sticky post when creating new posts in the future: https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/3332479#M1112153 

  • david_flu why do you want to add a calculated column? For sales and total sales, you can easily use measures which is the best way to go. What is the rational behind asking for a calculated column?

  • Daniel_PowerBI the solution danextian should work and you can also do this:

     

    add 2 new columns in each table and then set the relationship on these new columns, which will be one to many, one will be on the store/product side table. 

     

    //add new column in Sales Table
    Surrogate Key = SalesTable[Store] & SalesTable[Product]
    
    //add new column in Store Product table
    Surrogate Key = StoreProductTable[Store] & StoreProductTable[Product]
    
    //add new measure for sales
    Sales Measure = 
    SUMX (  SalesTable, SalesTable[Product Sales] & RELATED ( StoreProductTable[Unit Price] ) )

     

    and to visualize. use Week/Day/Store column from SalesTable and the measure Sales Measure

     

    Change the column names and table names as per your data model.

    • david_flu's avatar
      david_flu
      Frequent Visitor

      hi parry2k . i'm very very new to this and have a very little knowledge. thought that i need to add a column to write the lookup function in. will try your solution first and get back to you.

       

      many thansk

  • david_flu's avatar
    david_flu
    Frequent Visitor

    Hi danextian 

     

    Thanks for the help and apologies that i didn't explain clearly. please view tables below and i would like to use to add column function to have the daily summary of each product sales for each store  each week.

     

    WEEK       DAY        STORE      PRODUCT SALES AMOUNT TOTAL SALES

    week 1MONDAYEASTAPPLE120 
    week 1MONDAYEASTORANG129 
    week 1MONDAYEASTBANANA137 
    week 1MONDAYWESTAPPLE224 
    week 1MONDAYWESTORANG238 
    week 1MONDAYWESTBANANA208 
    week 1MONDAYNORTHAPPLE102 
    week 1MONDAYNORTHORANG87 
    week 1MONDAYNORTHBANANA89 
    week 1MONDAYSOUTHAPPLE172 
    week 1MONDAYSOUTHORANG170 
    week 1MONDAYSOUTHBANANA190 
    week 1MONDAYCENTRALAPPLE247 
    week 2MONDAYCENTRALORANG221 
    week 2MONDAYCENTRALBANANA232 

     

    STORE      PRODUCT   UNIT PRICE

    EASTAPPLE$3.30
    EASTORANG$4.20
    EASTBANANA$3.50
    WESTAPPLE$2.20
    WESTORANG$3.80
    WESTBANANA$2.90
    NORTHAPPLE$1.90
    NORTHORANG$2.30
    NORTHBANANA$1.80
    SOUTHAPPLE$3.30
    SOUTHORANG$5.20
    SOUTHBANANA$3.90
    CENTRALAPPLE$5.30
    CENTRALORANG$5.80
    CENTRALBANANA$5.20

     

     

    Many thanks

  • david_flu I don't think this will work or is the right approach, I told you to add calculated columns first to connect which I think you completely missed.

    • Daniel_PowerBI's avatar
      Daniel_PowerBI
      Icon for Advocate I rankAdvocate I

      Not sure what's going on here parry2k 

       

      @david_flu I don't think this will work or is the right approach, I told you to add calculated columns first to connect which I think you completely missed.

       

      david_flu why do you want to add a calculated column? For sales and total sales, you can easily use measures which is the best way to go. What is the rational behind asking for a calculated column?

  • Daniel_PowerBI david_flu I'm done with this post, everything is confusing - are you looking for a solution or going to give the suggestions without following the solution? Thank you!

    • david_flu's avatar
      david_flu
      Frequent Visitor

      Hi parry2k , I have followed your steps and still struggling with it. as i have mentioned before that i'm very new to this..

      The steps i have done was good until the last measure, somehow it can't find the column for the unit price. please advise where i have done wrong.

       

      i have created 2 unique tables for the relationship, 1) StoreTable. 2) ProductTable.

       

      step 1 below is where i created new column from the SalesTable.

       

      setp 2 below is new culomn created from the StoreProductTable

       

      step 3 below is in error as i don't know where i done wrong and it's located in the SalesTable

       

      all tables below

       

      below is the relationships page

       

       

      Thanks