Forum Discussion

TanzilHasan's avatar
TanzilHasan
Frequent Visitor
1 year ago
Solved

Group Customer by Order type

Hi All, 

 

I have a data set that includes customer ID, order number, order date, and order type. The order type has three possible values: free, paid, and mixed. For example, a single customer may place orders of all three types on different dates.

 

I want to analyze how many customers have placed orders in specific categories during a reporting period: only paid orders, only free orders, free and paid orders, and orders that include all three types. I attempted to achieve this using Power Query but got stuck along the way.

 

How do I solve this?

Sample data:

 

Customer IDOrder NoOrder DateOrder Type
CUST001ORD10011/11/2024Paid
CUST001ORD100210/11/2024Free
CUST001ORD100315/11/2024Mixed
CUST002ORD10043/11/2024Free
CUST002ORD100512/11/2024Paid
CUST003ORD10062/11/2024Mixed
CUST003ORD10078/11/2024Free
CUST003ORD100820/11/2024Paid
CUST004ORD10095/11/2024Paid
CUST004ORD101018/11/2024Mixed
CUST005ORD10117/11/2024Free
CUST005ORD101214/11/2024Paid
CUST006ORD10131/11/2024Mixed
CUST006ORD101411/11/2024Mixed
CUST007ORD10156/11/2024Free
CUST007ORD101619/11/2024Paid
CUST008ORD10174/11/2024Free
CUST008ORD101815/11/2024Paid
CUST009ORD10199/11/2024Mixed
CUST009ORD102021/11/2024Paid

 

Expected data:

Customer IDCustomer Type
CUST001All Three
CUST003All Three
CUST002Free and Paid
CUST005Free and Paid
CUST007Free and Paid
CUST008Free and Paid
CUST004Only Paid
CUST004Paid and Mixed
CUST009Paid and Mixed
  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi TanzilHasan,

    Thanks for reaching out Microsoft fabric community forum.


    As per your query, follow the below steps to achieve you goal.

    I used the above data. I imported data into power query editor and in transform data i have done group by customer id and order type. 

    A separate table is created for each customer 

    Add custom column .use the below code in formula bar 

    Text.Combine(List.Distinct([Order Types][Order Type]), ", ") 

    Add another custom column by using the below code in formula bar:

    if Text.Contains([Custom], "Paid") and Text.Contains([Custom], "Free") and Text.Contains([Custom], "Mixed") then "All Three"
    else if Text.Contains([Custom], "Paid") and Text.Contains([Custom], "Free") then "Free and Paid"
    else if Text.Contains([Custom], "Paid") and Text.Contains([Custom], "Mixed") then "Paid and Mixed"
    else if Text.Contains([Custom], "Paid") then "Only Paid"
    else if Text.Contains([Custom], "Free") then "Only Free"
    else "Other"

     

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

    Thank you 

     

5 Replies

  • Irwan's avatar
    Irwan
    Super User

    hello TanzilHasan 

     

    please check if this accomodate your need.

     

    create new table for summarize your original data.

    Summarize =
    SUMMARIZE(
        ADDCOLUMNS(
            SUMMARIZE(
                'Table',
                'Table'[Customer ID],
                "Type",
                CONCATENATEX(
                    'Table',
                    'Table'[Order Type],
                    " ",
                    'Table'[Index],
                    DESC
                )
            ),
            "Customer Type",
            IF(
                [Type]="Mixed Free Paid",
                "All Three",
                SUBSTITUTE(
                    [Type],
                    " ",
                    " and "
                )
            )
        ),
        'Table'[Customer ID],
        [Customer Type]
    )
     

    Hope this will help.

    Thank you.

  • TanzilHasan 

    Create a Calculated Columns:

    UniqueOrderTypes = 
    CONCATENATEX(
    DISTINCT('Orders Table'[Order Type]),
    'Orders Table'[Order Type],
    ", "
    )
    CustomerType = 
    SWITCH(
    TRUE(),
    'Orders Table'[UniqueOrderTypes] = "Free, Paid, Mixed", "All Three",
    'Orders Table'[UniqueOrderTypes] = "Free, Paid", "Free and Paid",
    'Orders Table'[UniqueOrderTypes] = "Paid, Mixed", "Paid and Mixed",
    'Orders Table'[UniqueOrderTypes] = "Free, Mixed", "Free and Mixed",
    'Orders Table'[UniqueOrderTypes] = "Paid", "Only Paid",
    'Orders Table'[UniqueOrderTypes] = "Free", "Only Free",
    'Orders Table'[UniqueOrderTypes] = "Mixed", "Only Mixed",
    "Other"
    )

    💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
    Cheers,
    Kedar
    Connect on LinkedIn

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi TanzilHasan,

    Thanks for reaching out Microsoft fabric community forum.


    As per your query, follow the below steps to achieve you goal.

    I used the above data. I imported data into power query editor and in transform data i have done group by customer id and order type. 

    A separate table is created for each customer 

    Add custom column .use the below code in formula bar 

    Text.Combine(List.Distinct([Order Types][Order Type]), ", ") 

    Add another custom column by using the below code in formula bar:

    if Text.Contains([Custom], "Paid") and Text.Contains([Custom], "Free") and Text.Contains([Custom], "Mixed") then "All Three"
    else if Text.Contains([Custom], "Paid") and Text.Contains([Custom], "Free") then "Free and Paid"
    else if Text.Contains([Custom], "Paid") and Text.Contains([Custom], "Mixed") then "Paid and Mixed"
    else if Text.Contains([Custom], "Paid") then "Only Paid"
    else if Text.Contains([Custom], "Free") then "Only Free"
    else "Other"

     

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

    Thank you