Forum Discussion

daicaboy's avatar
daicaboy
Helper I
2 years ago
Solved

Calculator column condition issue

Hi everybody,

I have a data like data1 and i want to count how many product meet the condition. If the number sale of product for each store more than 10 is the Good sales, and less is Bad Sales.

Data 1

StoreDateProductSale
NY01/01/2020Computer20
NY01/01/2020Phone2
NY05/10/2020Headphone5
LA01/02/2020Computer2
NY01/02/2020Phone4
LA10/05/2020Headphone1
LA01/02/2020Computer4
De06/07/2020Phone5
De09/08/2020Headphone6
De10/02/2020Headphone3

 

Report

StoreGood salesBad sales
NY  
LA  
De  

 

I'm so appreciate for any help from you guys. Thanks

  • Good day daicaboy,

    To classify the data in Power Query you could use this approach,

    1. Group the data by store, summing the "Sale" column (call this summed column "Good/Bad" in anticipation of the next step).
    2. Transform the summed sale column from a number to "Good" or "Bad" depending on the value of the summed sales.
    3. Expand the table.
    4. Create a matrix visual with "Store" on rows, "Good or Bad" on columns and a sum of sales for values.

    This yields,

    Here is code I used. I had copied your data into a table in Excel which I called "SalesTable" and then started by loading that table into Power BI Desktop.

    let
    Source = Excel.Workbook(File.Contents("PUT YOUR FILE SPECIFICAION HERE IF LOADING FROM EXCEL"), null, true),
    SalesTable_Table = Source{[Item="SalesTable",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(SalesTable_Table,{{"Store", type text}, {"Date", type date}, {"Product", type text}, {"Sale", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Store"}, {{"Good/Bad", each List.Sum([Sale]), type nullable number}, {"All Rows", each _, type table [Store=nullable text, Date=nullable date, Product=nullable text, Sale=nullable number]}}),
    #"Classify as Good/Bad" = Table.TransformColumns( #"Grouped Rows", {{"Good/Bad", each if _>10 then "Good" else "Bad"}} ),
    #"Expanded All Rows" = Table.ExpandTableColumn(#"Classify as Good/Bad", "All Rows", {"Date", "Product", "Sale"}, {"Date", "Product", "Sale"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded All Rows",{{"Good/Bad", type text}})
    in
    #"Changed Type1   

     Hope this helps

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi daicaboy ,

    Please follow these steps:

    1.Create calculated columns and write formulas.

    Column =
    
    var _stroe = 'Table'[Store]
    
    VAR _sum_sale = CALCULATE(SUM('Table'[Sale]),FILTER('Table',_stroe = 'Table'[Store]))
    
    RETURN
    
    IF(_sum_sale > 10,
    
    "Good sales",
    
    "Bad sales")

    2.Getting results.

    3.Create the matrix in the report view.

    If your Current Period does not refer to this, please clarify in a follow-up reply.

     

    Best Regards,

    Clara Gong

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

3 Replies

  • collinsg's avatar
    collinsg
    Solution Sage

    Good day daicaboy,

    To classify the data in Power Query you could use this approach,

    1. Group the data by store, summing the "Sale" column (call this summed column "Good/Bad" in anticipation of the next step).
    2. Transform the summed sale column from a number to "Good" or "Bad" depending on the value of the summed sales.
    3. Expand the table.
    4. Create a matrix visual with "Store" on rows, "Good or Bad" on columns and a sum of sales for values.

    This yields,

    Here is code I used. I had copied your data into a table in Excel which I called "SalesTable" and then started by loading that table into Power BI Desktop.

    let
    Source = Excel.Workbook(File.Contents("PUT YOUR FILE SPECIFICAION HERE IF LOADING FROM EXCEL"), null, true),
    SalesTable_Table = Source{[Item="SalesTable",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(SalesTable_Table,{{"Store", type text}, {"Date", type date}, {"Product", type text}, {"Sale", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Store"}, {{"Good/Bad", each List.Sum([Sale]), type nullable number}, {"All Rows", each _, type table [Store=nullable text, Date=nullable date, Product=nullable text, Sale=nullable number]}}),
    #"Classify as Good/Bad" = Table.TransformColumns( #"Grouped Rows", {{"Good/Bad", each if _>10 then "Good" else "Bad"}} ),
    #"Expanded All Rows" = Table.ExpandTableColumn(#"Classify as Good/Bad", "All Rows", {"Date", "Product", "Sale"}, {"Date", "Product", "Sale"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded All Rows",{{"Good/Bad", type text}})
    in
    #"Changed Type1   

     Hope this helps

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi daicaboy ,

    Please follow these steps:

    1.Create calculated columns and write formulas.

    Column =
    
    var _stroe = 'Table'[Store]
    
    VAR _sum_sale = CALCULATE(SUM('Table'[Sale]),FILTER('Table',_stroe = 'Table'[Store]))
    
    RETURN
    
    IF(_sum_sale > 10,
    
    "Good sales",
    
    "Bad sales")

    2.Getting results.

    3.Create the matrix in the report view.

    If your Current Period does not refer to this, please clarify in a follow-up reply.

     

    Best Regards,

    Clara Gong

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

  • Thanks you all. Both work fine for me. You guys help me a lot.