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
cpdanielmc21
Helper I
Helper I

Conditional custom column with some math

Hi community,

Hope you can help me on this one.

 

I want to create a custom column in query editor based on some conditions which include summation and split of amounts. I was able to do this with excel, but can't find a way in powerBI so far...

 

Let me explain: if the sum of amounts for categories 1 and 2 are negative (if they share same month), then category 1 should be zero and the sum of it's amounts must be splited and added into the raws of category 2. Other categories (like 3) untouched.

Capture11.JPG

 

But, if the sum of categories 1 and 2 are positive (if they share month), then categories 1 and 2 must show zero and other categories untouched.

Capture12.JPG

 

Also, number of raws here is very simple, but my data can have hundreds of raws, so the split must be dynamic depending on the number of raws for category 2 and only for the corresponding month. 

 

Any ideas? you think this can be possible?

 

 

 

2 ACCEPTED SOLUTIONS

Hi @cpdanielmc21 ,

 

Thanks for explaining, I made this, hope that's what you want:

Measure 14 = 
VAR sumcategory = CALCULATE(SUM('Table (4)'[Amount]),FILTER(ALL('Table (4)'[Category],'Table (4)'[Amount]),'Table (4)'[Category] in {1,2}))
VAR sumcategory1 = CALCULATE(SUM('Table (4)'[Amount]),FILTER(ALL('Table (4)'[Category],'Table (4)'[Amount]),'Table (4)'[Category] in {1}))
VAR countrow2 = CALCULATE(COUNTROWS('Table (4)'),FILTER(ALL('Table (4)'[Category],'Table (4)'[Amount]),'Table (4)'[Category] = 2))
RETURN IF(MAX('Table (4)'[Category]) = 3,SUM('Table (4)'[Amount]),
        IF(sumcategory < 0,
            IF(MAX('Table (4)'[Category]) = 1,0,sumcategory1 / countrow2 + SUM('Table (4)'[Amount])),
            0))

 

Conditional custom column with some math.PNG

 

Aiolos Zhao





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

v-lionel-msft
Community Support
Community Support

Hi @cpdanielmc21 ,

 

You can modify @AiolosZhao 's code and create a calculated column directly.

Column = 
VAR sumcategory = 
CALCULATE(
    SUM('Table (4)'[Amount]),
    FILTER(
       ALL( 'Table (4)'),
        'Table (4)'[Category] in {1,2} && 'Table (4)'[Month] = EARLIER('Table (4)'[Month] )
    )
)
VAR sumcategory1 = 
CALCULATE(
    SUM('Table (4)'[Amount]),
    FILTER(
        ALL('Table (4)'),
        'Table (4)'[Category] = 1 && 'Table (4)'[Month] = EARLIER('Table (4)'[Month] )
    )
)
VAR countrow2 = 
CALCULATE(
    COUNTROWS('Table (4)'),
    FILTER(
        ALL('Table (4)'),
        'Table (4)'[Category] = 2 && 'Table (4)'[Month] = EARLIER('Table (4)'[Month] )
    )
)
RETURN 
IF(
    [Category] = 3,
    'Table (4)'[Amount],
    IF(
        sumcategory > 0,
        0,
        SWITCH(
            [Category],
            1, 0,
            2, (sumcategory1/countrow2) + 'Table (4)'[Amount]
        )
    )
)

nnn5.PNGnnn6.PNG

 

Best regards,
Lionel Chen

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

View solution in original post

8 REPLIES 8
v-lionel-msft
Community Support
Community Support

Hi @cpdanielmc21 ,

 

You can modify @AiolosZhao 's code and create a calculated column directly.

Column = 
VAR sumcategory = 
CALCULATE(
    SUM('Table (4)'[Amount]),
    FILTER(
       ALL( 'Table (4)'),
        'Table (4)'[Category] in {1,2} && 'Table (4)'[Month] = EARLIER('Table (4)'[Month] )
    )
)
VAR sumcategory1 = 
CALCULATE(
    SUM('Table (4)'[Amount]),
    FILTER(
        ALL('Table (4)'),
        'Table (4)'[Category] = 1 && 'Table (4)'[Month] = EARLIER('Table (4)'[Month] )
    )
)
VAR countrow2 = 
CALCULATE(
    COUNTROWS('Table (4)'),
    FILTER(
        ALL('Table (4)'),
        'Table (4)'[Category] = 2 && 'Table (4)'[Month] = EARLIER('Table (4)'[Month] )
    )
)
RETURN 
IF(
    [Category] = 3,
    'Table (4)'[Amount],
    IF(
        sumcategory > 0,
        0,
        SWITCH(
            [Category],
            1, 0,
            2, (sumcategory1/countrow2) + 'Table (4)'[Amount]
        )
    )
)

nnn5.PNGnnn6.PNG

 

Best regards,
Lionel Chen

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

Hi @v-lionel-msft  and @AiolosZhao 

 

Both solutions work in different scenarios so I am using both, thank you!

amitchandak
Super User
Super User

@cpdanielmc21 , try new columns like


custom 1 = if([category] in{ 1,2} && [Amount] <0)
custom 2 = if([category] in{ 1,2} && [Amount] >0)

AiolosZhao
Memorable Member
Memorable Member

Hi @cpdanielmc21 ,

 

I think using DAX doing the logic is not hard, but i don't get the logic for split amount.

 

 In scenario one, the total amount = -21. Then you want to split it to 2 rows(amount -80 and amount -1)

 

Why the custom number is -50 and 29?

 

Could you please help to explain how you get -50 and 29?

 

Thanks.

Aiolos Zhao





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




@AiolosZhao 

Hi, sure

 

total for category 1 is 60, so this 60 must be assigned to category 2. 
Since category 2 has 2 rows, the split is going to be 30 and 30.

 

-80 plus 30 = -50

-1 plus 30 = 29

 

 

so before this process, total for cat 1 and 2 was -21, and after the process is still -21, is just re-arranged.

Hi @cpdanielmc21 ,

 

Thanks for explaining, I made this, hope that's what you want:

Measure 14 = 
VAR sumcategory = CALCULATE(SUM('Table (4)'[Amount]),FILTER(ALL('Table (4)'[Category],'Table (4)'[Amount]),'Table (4)'[Category] in {1,2}))
VAR sumcategory1 = CALCULATE(SUM('Table (4)'[Amount]),FILTER(ALL('Table (4)'[Category],'Table (4)'[Amount]),'Table (4)'[Category] in {1}))
VAR countrow2 = CALCULATE(COUNTROWS('Table (4)'),FILTER(ALL('Table (4)'[Category],'Table (4)'[Amount]),'Table (4)'[Category] = 2))
RETURN IF(MAX('Table (4)'[Category]) = 3,SUM('Table (4)'[Amount]),
        IF(sumcategory < 0,
            IF(MAX('Table (4)'[Category]) = 1,0,sumcategory1 / countrow2 + SUM('Table (4)'[Amount])),
            0))

 

Conditional custom column with some math.PNG

 

Aiolos Zhao





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




@AiolosZhao Thanks!

 

One question, is there a way to do this directly as a custom column in the Data / step in query editor instead of measure?

 

Also, the measure works very well except when I start to filter:

So the thing is, later on I will create a summary with the total values per month of the categories, and this is what I get (Category 1 is zero, and 3 is 100 that's ok, but total category 2 should be -21)

Capture13.JPG

 

 

@cpdanielmc21 ,

 

I think it's another logic in your screenshot.

 

Do you want to show the amount column by sum or not sum?

 

In your screenshot, the amount is a sum, so every category will only have one amount.

 

But in your post, you split the category 1 amount to 3 rows, that's different. which one is you wanted?

 

Aiolos Zhao





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




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.