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
fitconsulting
Frequent Visitor

VALORATED CONSUMPTION

I’m having trouble getting a query, could you please help me?

 

I have one first table with monthly stock consumption per product (CONSUMPTION TABLE).

 

I also have another table with monthly purchases as the following. In this case I got the last monthly purchase (PURCHASES TABLE).

 

Finally, I would need to get a table in which I have monthly products with their respective costs and consumption, and this is where I am having the most trouble (VALORATED CONSUMPTION)

 

 

 

CONSUMPTION

Month

Article

Stock Consumption

January

A

10

February

B

8

February

A

30

February

B

20

March

A

40

March

B

30

April

A

25

April

B

6

 

 

 

 

 

PURCHASES

Month

Article

Cost

January

A

-2

January

B

-3

February

A

-5

March

B

-8

April

A

-10

 

 

 

 

 

VALORATED CONSUMPTION TABLE

Month

Article

Cost

Unitary Cost

Valorated Consumption

January

A

10

-2

-20

February

B

8

-3

-24

February

A

30

-5

-150

February

B

20

-3

-60

March

A

40

-5

-200

March

B

30

-8

-240

April

A

25

-10

-250

April

B

6

-8

-48

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

Hi @fitconsulting,

 

One sample for your refernce.

 

1. Create two calculated columns in your tables.

 

Monthno = SWITCH('CONSUMPTION'[Month],"January",1,"February",2,"March",3,"April",4)
MonthNo = SWITCH(PURCHASES[Month],"January",1,"February",2,"March",3,"April",4)

2. Create the new calculated columns in CONSUMPTION table as below.

 

Unitary Cost = 
VAR _same =
    CALCULATE (
        SUM ( 'PURCHASES'[Cost] ),
        FILTER (
            PURCHASES,
            PURCHASES[Month] = 'CONSUMPTION'[Month]
                && PURCHASES[Article] = 'CONSUMPTION'[Article]
        )
    )
VAR _maxno =
    CALCULATE (
        MAX ( PURCHASES[MonthNo] ),
        FILTER (
            PURCHASES,
            'PURCHASES'[Article] = CONSUMPTION[Article]
                && 'PURCHASES'[MonthNo] < 'CONSUMPTION'[Monthno]
        )
    )
RETURN
    IF (
        ISBLANK ( _same ),
        CALCULATE (
            SUM ( PURCHASES[Cost] ),
            FILTER (
                PURCHASES,
                PURCHASES[Article] = CONSUMPTION[Article]
                    && 'PURCHASES'[MonthNo] = _maxno
            )
        ),
        _same
    )
Valorated Consumption = CONSUMPTION[Unitary Cost]*CONSUMPTION[Stock Consumption]


Then we can get the result as we need. Please find the pbix as attached for your reference.

 

 Capture.PNG

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

1 REPLY 1
v-frfei-msft
Community Support
Community Support

Hi @fitconsulting,

 

One sample for your refernce.

 

1. Create two calculated columns in your tables.

 

Monthno = SWITCH('CONSUMPTION'[Month],"January",1,"February",2,"March",3,"April",4)
MonthNo = SWITCH(PURCHASES[Month],"January",1,"February",2,"March",3,"April",4)

2. Create the new calculated columns in CONSUMPTION table as below.

 

Unitary Cost = 
VAR _same =
    CALCULATE (
        SUM ( 'PURCHASES'[Cost] ),
        FILTER (
            PURCHASES,
            PURCHASES[Month] = 'CONSUMPTION'[Month]
                && PURCHASES[Article] = 'CONSUMPTION'[Article]
        )
    )
VAR _maxno =
    CALCULATE (
        MAX ( PURCHASES[MonthNo] ),
        FILTER (
            PURCHASES,
            'PURCHASES'[Article] = CONSUMPTION[Article]
                && 'PURCHASES'[MonthNo] < 'CONSUMPTION'[Monthno]
        )
    )
RETURN
    IF (
        ISBLANK ( _same ),
        CALCULATE (
            SUM ( PURCHASES[Cost] ),
            FILTER (
                PURCHASES,
                PURCHASES[Article] = CONSUMPTION[Article]
                    && 'PURCHASES'[MonthNo] = _maxno
            )
        ),
        _same
    )
Valorated Consumption = CONSUMPTION[Unitary Cost]*CONSUMPTION[Stock Consumption]


Then we can get the result as we need. Please find the pbix as attached for your reference.

 

 Capture.PNG

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

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.