Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Group orderlines by order no. and filter

Hi all 🙂

 

I have a sales table that looks like this:

OrderlineOrder Number Sales ProfitDiscountAdjustedYearMonth
11510Manually2020-05
2125 2020-05
321015Manually2020-05
4238Manually2020-06
532010Manually2020-05

 

I would like to group the order numbers and count the profit discount % per distinct order number, but only include orderlines with "Manually" in the adjusted column.

 

The profit discount % = Discount / (Sales Profit + Discount) 

 

Essentially i need the table to look like this below or somehow make a measure that can count order colors by month given a filter on "adjusted" and categorize orders by profit discount. (just whatever is possible or easiest).

 

Order Number Sales ProfitDiscountAdjustedYearMonth

Profit discount_%

order_color

1510Manually2020-0566,6%Red
21323Manually2020-0563,8%Red
32010Manually2020-0533,3%Orange

 

Then I would like to show count of distinct order numbers per month categorized into 3 groups based on the Profit Discount  % and if orderlines within same order number is in 2 months, just take the earliest month.

Between 0-25% profit discount = BLUE

Between 25-50% profit discount = ORANGE

Above 50% profit discount = RED

 

In this sample case the graph would show 2 red and 1 orange in 2020-05

 

 

Hope it makes sense. Would really appreciate your help. thanks!

1 ACCEPTED SOLUTION
v-eqin-msft
Community Support
Community Support

Hi @Anonymous ,

I did it in two ways.

 

1. Create measures:

Discount Measure =
CALCULATE (
    SUM ( 'Sales'[Discount] ),
    FILTER (
        'Sales',
        'Sales'[Order Number ] = MAX ( 'Sales'[Order Number ] )
            && 'Sales'[Adjusted] = "Manually"
    )
)


Profit =
CALCULATE (
    SUM ( 'Sales'[Sales Profit] ),
    FILTER (
        'Sales',
        'Sales'[Order Number ] = MAX ( 'Sales'[Order Number ] )
            && 'Sales'[Adjusted] = "Manually"
    )
)


Profit discount_% =
[Discount Measure] / ( [Discount Measure] + [Profit] )


Year-Month =
CALCULATE (
    MIN ( 'Sales'[YearMonth] ),
    FILTER (
        'Sales',
        'Sales'[Order Number ] = MAX ( 'Sales'[Order Number ] )
            && 'Sales'[Adjusted] = "Manually"
    )
)


order_color =
IF (
    [Profit discount_%] >= 0
        && [Profit discount_%] <= 0.25,
    "Blue",
    IF ( [Profit discount_%] > 0.25 && [Profit discount_%] <= 0.5, "Orange", "Red" )
)

 

2. Create a new calculated table:

Table =
VAR _t =
    DISTINCT ( 'Sales'[Order Number ] )
RETURN
    ADDCOLUMNS (
        _t,
        "Sales",
            VAR _disOrder = 'Sales'[Order Number ]
            RETURN
                SUMX (
                    FILTER (
                        ALL ( 'Sales' ),
                        [Adjusted] = "Manually"
                            && [Order Number ] = _disOrder
                    ),
                    [Sales Profit]
                ),
        "Profit Discount",
            VAR _disOrder = 'Sales'[Order Number ]
            RETURN
                SUMX (
                    FILTER (
                        ALL ( 'Sales' ),
                        [Adjusted] = "Manually"
                            && [Order Number ] = _disOrder
                    ),
                    [Discount]
                ),
        "Adjusted", "Manually",
        "YearMonth",
            VAR _disOrder = 'Sales'[Order Number ]
            RETURN
                MINX (
                    FILTER (
                        ALL ( 'Sales' ),
                        [Adjusted] = "Manually"
                            && [Order Number ] = _disOrder
                    ),
                    [YearMonth]
                )
)

Then add a column to it:

Profit discount_% =
[Profit Discount] / ( [Profit Discount] + [Sales] )

And creata a measure for conditional formatting:

Order color =
IF (
    MAX ( 'Table'[Profit discount_%] ) >= 0
        && MAX ( 'Table'[Profit discount_%] ) <= 0.25,
    "Blue",
    IF (
        MAX ( 'Table'[Profit discount_%] ) > 0.25
            && MAX ( 'Table'[Profit discount_%] ) <= 0.5,
        "Orange",
        "Red"
    )
)

The final output looks like this:

 

12.4.1.1.PNG

Here is the pbix file.

 

Did I answer your question ? Please mark my reply as solution. Thank you very much.

If not, please upload some insensitive data samples and expected output.

 

Best Regards,

Eyelyn Qin

View solution in original post

2 REPLIES 2
v-eqin-msft
Community Support
Community Support

Hi @Anonymous ,

I did it in two ways.

 

1. Create measures:

Discount Measure =
CALCULATE (
    SUM ( 'Sales'[Discount] ),
    FILTER (
        'Sales',
        'Sales'[Order Number ] = MAX ( 'Sales'[Order Number ] )
            && 'Sales'[Adjusted] = "Manually"
    )
)


Profit =
CALCULATE (
    SUM ( 'Sales'[Sales Profit] ),
    FILTER (
        'Sales',
        'Sales'[Order Number ] = MAX ( 'Sales'[Order Number ] )
            && 'Sales'[Adjusted] = "Manually"
    )
)


Profit discount_% =
[Discount Measure] / ( [Discount Measure] + [Profit] )


Year-Month =
CALCULATE (
    MIN ( 'Sales'[YearMonth] ),
    FILTER (
        'Sales',
        'Sales'[Order Number ] = MAX ( 'Sales'[Order Number ] )
            && 'Sales'[Adjusted] = "Manually"
    )
)


order_color =
IF (
    [Profit discount_%] >= 0
        && [Profit discount_%] <= 0.25,
    "Blue",
    IF ( [Profit discount_%] > 0.25 && [Profit discount_%] <= 0.5, "Orange", "Red" )
)

 

2. Create a new calculated table:

Table =
VAR _t =
    DISTINCT ( 'Sales'[Order Number ] )
RETURN
    ADDCOLUMNS (
        _t,
        "Sales",
            VAR _disOrder = 'Sales'[Order Number ]
            RETURN
                SUMX (
                    FILTER (
                        ALL ( 'Sales' ),
                        [Adjusted] = "Manually"
                            && [Order Number ] = _disOrder
                    ),
                    [Sales Profit]
                ),
        "Profit Discount",
            VAR _disOrder = 'Sales'[Order Number ]
            RETURN
                SUMX (
                    FILTER (
                        ALL ( 'Sales' ),
                        [Adjusted] = "Manually"
                            && [Order Number ] = _disOrder
                    ),
                    [Discount]
                ),
        "Adjusted", "Manually",
        "YearMonth",
            VAR _disOrder = 'Sales'[Order Number ]
            RETURN
                MINX (
                    FILTER (
                        ALL ( 'Sales' ),
                        [Adjusted] = "Manually"
                            && [Order Number ] = _disOrder
                    ),
                    [YearMonth]
                )
)

Then add a column to it:

Profit discount_% =
[Profit Discount] / ( [Profit Discount] + [Sales] )

And creata a measure for conditional formatting:

Order color =
IF (
    MAX ( 'Table'[Profit discount_%] ) >= 0
        && MAX ( 'Table'[Profit discount_%] ) <= 0.25,
    "Blue",
    IF (
        MAX ( 'Table'[Profit discount_%] ) > 0.25
            && MAX ( 'Table'[Profit discount_%] ) <= 0.5,
        "Orange",
        "Red"
    )
)

The final output looks like this:

 

12.4.1.1.PNG

Here is the pbix file.

 

Did I answer your question ? Please mark my reply as solution. Thank you very much.

If not, please upload some insensitive data samples and expected output.

 

Best Regards,

Eyelyn Qin

littlemojopuppy
Community Champion
Community Champion

Here's some code...

Order Count:=CALCULATE(
		COUNTROWS(Orders),
		FILTER(
			Orders,
			[Adjusted] = "Manually"
		)
	)


Total Sales Profit:=CALCULATE(
		SUM(Orders[Sales Profit]),
		FILTER(
			Orders,
			[Adjusted] = "Manually"
		)
	)


Total Discount:=CALCULATE(
		SUM(Orders[Discount]),
		FILTER(
			Orders,
			[Adjusted] = "Manually"
		)
	)


Profit Discount %:=DIVIDE(
		[Total Discount],
		[Total Sales Profit] + [Total Discount],
		BLANK()
	)


Order Color:=SWITCH(
	TRUE(),
	AND(
		[Profit Discount %] >=  0,
		[Profit Discount %] < .25
	), "BLUE",
	AND(
		[Profit Discount %] >= .25,
		[Profit Discount %] < .50
	), "ORANGE",
	"RED"
)

 

littlemojopuppy_0-1606920533980.png

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.