Forum Discussion

labuser1235's avatar
labuser1235
Helper IV
6 years ago
Solved

Rate Per Hour

Hi All,

 

I have two tables here with almost similar kind of data. However when I try to give relations there are two columns which needs to get matched for the end result. Which one to be taken as active and other one as inactive, either ways it goes wrong in the output table. PBIX LINK 

Please help.

 

Table1:

DateFruit NameTypePrice SoldTime taken to sold in min
1/31/2020OrangeType-2768
1/31/2020Apple Type-3834
1/31/2020GrapesType-1212
1/31/2020OrangeType-1565
1/31/2020OrangeType-31028
1/31/2020Apple Type-1315
1/31/2020GrapesType-2495
1/31/2020GrapesType-3647
1/31/2020OrangeType-1532
1/31/2020OrangeType-2712
1/31/2020Apple Type-3816
1/31/2020GrapesType-1224
1/30/2020OrangeType-153
1/30/2020OrangeType-31028
1/30/2020Apple Type-1334
1/30/2020GrapesType-247
1/30/2020GrapesType-3617
1/30/2020OrangeType-1530

 

 

Table2:

Fruit NameTypePrice
OrangeType-15
OrangeType-27
OrangeType-310
AppleType-13
AppleType-25
AppleType-38
GrapesType-12
GrapesType-24
GrapesType-36

 

Output Matrix Table: 

  

Fruit NameTypePriceAvg TimeRate per hour
OrangeType-1 $   5.0032.5 $       9.23
OrangeType-2 $   7.0040 $    10.50
OrangeType-3 $ 10.0028 $    21.43
AppleType-1 $   3.0024.5 $       7.35
AppleType-2 $   5.000 
AppleType-3 $   8.0025 $    19.20
GrapesType-1 $   2.0018 $       6.67
GrapesType-2 $   4.0051 $       4.71
GrapesType-3 $   6.0032 $    11.25
  • Anonymous's avatar
    Anonymous
    6 years ago

    Hi,

     

    To get the results, actually you don't need any relationship between the two tables. Delete the relationships.

     

    Then add the following calculated columns to your Table 2:

     

    AvgTime = AVERAGEX(
                FILTER('Demo data',
                        AND('Demo data'[Fruit Name]='Fruit Price'[Fruit Name],
                                    'Demo data'[Type]='Fruit Price'[Type])
                ),'Demo data'[Time taken to sold in min])

     

    Rate Per Hour = DIVIDE('Fruit Price'[Price],'Fruit Price'[AvgTime],0)*60

     

    This two calculated columns in Table 2 will give you the desire out except for "Apple". Because in one table it is spelt as "Apple" and in the other table it is spelt as "Apple " - with an extra space. Once you correct that spellings in your source table, it will work fine.

     

  • Hello labuser1235

     

    There are various ways to approach this. Personally, I try to avoid many-to-many relationship (if it is required) in Power BI.

     

    Alternate way is to create filter tables with unique values and then create One-to-Many relationship:

     

     

    I have used DAX to create these two tables:

     

     

     

    ftFruit = DISTINCT(dtFruits[Fruit Name])
    
    ftFruit = DISTINCT(dtFruits[Fruit Name])
    
    Created another DAX for Rate/hour
    
    Rate per Hour = DIVIDE(SUM(dtSales[Price Sold]),SUM(dtSales[Time taken to sold in min]))*60

     

    Following is the output:

     

     

    You may find the solution pbix file here

     

    Regards,
    Vivek

    If it helps, please mark it as a solution

    Kudos would be a cherry on the top 🙂

    https://www.vivran.in/

     

6 Replies

    • labuser1235's avatar
      labuser1235
      Helper IV

      Awesome worked like charm. Thank you so much 🙂

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi,

     

    To get the results, actually you don't need any relationship between the two tables. Delete the relationships.

     

    Then add the following calculated columns to your Table 2:

     

    AvgTime = AVERAGEX(
                FILTER('Demo data',
                        AND('Demo data'[Fruit Name]='Fruit Price'[Fruit Name],
                                    'Demo data'[Type]='Fruit Price'[Type])
                ),'Demo data'[Time taken to sold in min])

     

    Rate Per Hour = DIVIDE('Fruit Price'[Price],'Fruit Price'[AvgTime],0)*60

     

    This two calculated columns in Table 2 will give you the desire out except for "Apple". Because in one table it is spelt as "Apple" and in the other table it is spelt as "Apple " - with an extra space. Once you correct that spellings in your source table, it will work fine.

     

  • vivran22's avatar
    vivran22
    Community Champion

    Hello labuser1235

     

    There are various ways to approach this. Personally, I try to avoid many-to-many relationship (if it is required) in Power BI.

     

    Alternate way is to create filter tables with unique values and then create One-to-Many relationship:

     

     

    I have used DAX to create these two tables:

     

     

     

    ftFruit = DISTINCT(dtFruits[Fruit Name])
    
    ftFruit = DISTINCT(dtFruits[Fruit Name])
    
    Created another DAX for Rate/hour
    
    Rate per Hour = DIVIDE(SUM(dtSales[Price Sold]),SUM(dtSales[Time taken to sold in min]))*60

     

    Following is the output:

     

     

    You may find the solution pbix file here

     

    Regards,
    Vivek

    If it helps, please mark it as a solution

    Kudos would be a cherry on the top 🙂

    https://www.vivran.in/

     

    • labuser1235's avatar
      labuser1235
      Helper IV

      Thank you so much vivran22 . Didn't think to have seperate tables even this method is working.