Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Calculation between three Fact Tables

Hello,

 

I have four tables:

 

fStock (Aggregated at Vendor and Product Level)

- SK_VENDOR

- SK_PRODUCT

- Stock Available

 

SK_VENDORSK_PRODUCTStock Available
12310050000
12310125000
1241103000
1241113500

 

fSales (Aggregated at Vendor and Product Level)

- SK_VENDOR

- SK_PRODUCT

- L7D Items Sold

 

SK_VENDORSK_PRODUCTL7D ITEMS SOLD
123100250
123101300
123102400
1241031000
1241051200

 

fPurchaseOrderItem ( Aggregated at Purchase Order, Vendor and Product Level)

- SK_PURCHASE_ORDER

- SK_VENDOR

- SK_PRODUCT

- Quantity Ordered

 

SK_PURCHASE_ORDERSK_VENDORSK_PRODUCTQuantity Ordered
1123100500
1123101200
1123102300
1123129100

 

fInventory(Aggregated at Vendor and Product Level)

- SK_VENDOR

- SK_PRODUCT

- Units in Inventory

- Cost per Unit

SK_VENDORSK_PRODUCTUnits in InventoryCost per Unit
12310040010
12310130020
12310210030
124100129934

 

 

 

What I need?

- Calculate the Stock Coverage per Days : fStock(Stock Available)/fSales(L7D Items Sold)/7

- Calculate the Stock Value for Products below 30 days of Coverage: fInventory(Units in Inventory)/fInventory(Cost per Unit) for Products where Coverage > 30 Days

- Finally, I need to show the Stock Value in a table Grouped by Purchase Order

 

Example:

SK_PURCHASE_ORDERSK_VENDORQUANTITY ORDEREDStock Value from Supplier > 30 days Coverage
1123100010000
212330010000

 

Is it possible with a calculated measure?

 

Important Notes:

- Coverage should be calculaged upon the combination of Vendor and Product

- One PO can contain multiple Products, but it is always from the same Vendor.

 

Thanks

Carlos

  • Hi , Anonymous 

    Here are my test you can refer to :

    (1)For your first question. We can create a calculated column in 'fStock' Table :

    Stock Coverage per Days = var _current_vendor = 'fStock'[SK_VENDOR]
    var _current_product = 'fStock'[SK_PRODUCT]
    var _t2= FILTER('fSales','fSales'[SK_VENDOR]=_current_vendor && 'fSales'[SK_PRODUCT] = _current_product)
    return 
    DIVIDE( 'fStock'[Stock Available] , SUMX(_t2,[L7D ITEMS SOLD]) * 7 )

     The result is as follows:

    (2)For your second question . We can create a measure :

    Measure = var _current_vendor = SELECTEDVALUE('fInventory'[SK_VENDOR])
    var _current_product = SELECTEDVALUE('fInventory'[SK_PRODUCT])
    var _ts =MAXX(FILTER(ALLSELECTED('fStock') , 'fStock'[SK_VENDOR]+_current_vendor  && 'fStock'[SK_PRODUCT]=_current_product),[Stock Coverage per Days])
    return 
    IF( _ts > 30  , DIVIDE(SUM('fInventory'[Units in Inventory]) , SUM('fInventory'[Cost per Unit]) ,BLANK()))

    The result is as follows :

    (3)For you last question . We can create a table:

    Table = var _greate30 =DISTINCT( SELECTCOLUMNS( FILTER('fStock', 'fStock'[Stock Coverage per Days] >30 ) , "VENDOR" , [SK_VENDOR]))
    var  _po_t =FILTER('fPurchaseOrderItem' , 'fPurchaseOrderItem'[SK_VENDOR] in  _greate30)
    return 
    SUMMARIZE(_po_t , [SK_PURCHASE_ORDER] , [SK_VENDOR] , "QUANTITY ORDERED" ,SUM('fPurchaseOrderItem'[Quantity Ordered]))

    And then we can create a calculated column :

    Stock Value from Supplier > 30 days Coverage = var _current_vendor = 'Table'[SK_VENDOR] 
    var _t =SUMX( FILTER( 'fStock' , 'fStock'[Stock Coverage per Days]> 30 &&  'fStock'[SK_VENDOR]=_current_vendor ) , [Stock Available])
    return _t

    The result is as follows:

     

     

    If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.

     

    Best Regards,

    Aniya Zhang

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

     

     

     

2 Replies

  • Hi , Anonymous 

    Here are my test you can refer to :

    (1)For your first question. We can create a calculated column in 'fStock' Table :

    Stock Coverage per Days = var _current_vendor = 'fStock'[SK_VENDOR]
    var _current_product = 'fStock'[SK_PRODUCT]
    var _t2= FILTER('fSales','fSales'[SK_VENDOR]=_current_vendor && 'fSales'[SK_PRODUCT] = _current_product)
    return 
    DIVIDE( 'fStock'[Stock Available] , SUMX(_t2,[L7D ITEMS SOLD]) * 7 )

     The result is as follows:

    (2)For your second question . We can create a measure :

    Measure = var _current_vendor = SELECTEDVALUE('fInventory'[SK_VENDOR])
    var _current_product = SELECTEDVALUE('fInventory'[SK_PRODUCT])
    var _ts =MAXX(FILTER(ALLSELECTED('fStock') , 'fStock'[SK_VENDOR]+_current_vendor  && 'fStock'[SK_PRODUCT]=_current_product),[Stock Coverage per Days])
    return 
    IF( _ts > 30  , DIVIDE(SUM('fInventory'[Units in Inventory]) , SUM('fInventory'[Cost per Unit]) ,BLANK()))

    The result is as follows :

    (3)For you last question . We can create a table:

    Table = var _greate30 =DISTINCT( SELECTCOLUMNS( FILTER('fStock', 'fStock'[Stock Coverage per Days] >30 ) , "VENDOR" , [SK_VENDOR]))
    var  _po_t =FILTER('fPurchaseOrderItem' , 'fPurchaseOrderItem'[SK_VENDOR] in  _greate30)
    return 
    SUMMARIZE(_po_t , [SK_PURCHASE_ORDER] , [SK_VENDOR] , "QUANTITY ORDERED" ,SUM('fPurchaseOrderItem'[Quantity Ordered]))

    And then we can create a calculated column :

    Stock Value from Supplier > 30 days Coverage = var _current_vendor = 'Table'[SK_VENDOR] 
    var _t =SUMX( FILTER( 'fStock' , 'fStock'[Stock Coverage per Days]> 30 &&  'fStock'[SK_VENDOR]=_current_vendor ) , [Stock Available])
    return _t

    The result is as follows:

     

     

    If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.

     

    Best Regards,

    Aniya Zhang

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

     

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello

      How are you so good on DAX ?

      Thanks a lot