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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
fleal
Frequent Visitor

Find the last day value;

Please, I need some help.

 

I intend to calculete the day by day price variation between the values of public traded stocks using DAX.After hour of reserch, I constructed the following expression, that gave me the current and the last price:

 

 

SUMMARIZE (
    ADDCOLUMNS (
        Inf_diario,
        "PREVIOUS VL",
        VAR previousDate =
            MAXX (
                FILTER (
                    Inf_diario,
                    Inf_diario[CNPJ_FUNDO] = EARLIER ( Inf_diario[CNPJ_FUNDO] )
                        && Inf_diario[DT_COMPTC] < EARLIER ( Inf_diario[DT_COMPTC] )
                ),
                Inf_diario[DT_COMPTC]
            )
        RETURN
            MAXX (
                FILTER (
                    Inf_diario,
                    Inf_diario[CNPJ_FUNDO] = EARLIER ( Inf_diario[CNPJ_FUNDO] )
                        && Inf_diario[DT_COMPTC] = previousDate
                ),
                Inf_diario[VL_QUOTA]
            )
    ),
    Inf_diario[ID],
    Inf_diario[DT_COMPTC],
    Inf_diario[VL_QUOTA],
    [PREVIOUS VL]
)

 

The results:

 Captura_DAX.JPG

Now, I need to find the diference between the PREVIOUS VL and VL QUOTA (current value), to finally calculate Standard deviation, %change etc.

 

Tks in advance

1 ACCEPTED SOLUTION
mattbrice
Solution Sage
Solution Sage

It seems you are modeling off a single table and created a second calculated table. If so, i would do things a bit differently.  I would use measures instead.   First, put 'id' and 'DT_COMPTC' on rows of Matrix, then add the following measures:

 

VL_Quota measure = SUM (Inf_diario[VL_Quota] )

then:

 

Previous VL Measure =
VAR _dates =
    FILTER (
        ALL ( Inf_diario[DT_COMPTC] ),
        Inf_diario[DT_COMPTC] < MAX ( Inf_diario[DT_COMPTC] )
    )
VAR _previousDate =
    LASTNONBLANK ( _dates, [VL_Quota measure] )
RETURN
    CALCULATE ( [VL_Quota measure], Inf_diario[DT_CoMPTC] = _previousDate )

then your % change is easy:

 

% Change =
DIVIDE ( [Previous DL Measure] - [VL_Quota measure], [VL_Quota measure] )

and so on.

 

But if you want to continue to use your calculated table, you'll need to add more values via the ADDCOLUMNS you have.

 

 

View solution in original post

2 REPLIES 2
mattbrice
Solution Sage
Solution Sage

It seems you are modeling off a single table and created a second calculated table. If so, i would do things a bit differently.  I would use measures instead.   First, put 'id' and 'DT_COMPTC' on rows of Matrix, then add the following measures:

 

VL_Quota measure = SUM (Inf_diario[VL_Quota] )

then:

 

Previous VL Measure =
VAR _dates =
    FILTER (
        ALL ( Inf_diario[DT_COMPTC] ),
        Inf_diario[DT_COMPTC] < MAX ( Inf_diario[DT_COMPTC] )
    )
VAR _previousDate =
    LASTNONBLANK ( _dates, [VL_Quota measure] )
RETURN
    CALCULATE ( [VL_Quota measure], Inf_diario[DT_CoMPTC] = _previousDate )

then your % change is easy:

 

% Change =
DIVIDE ( [Previous DL Measure] - [VL_Quota measure], [VL_Quota measure] )

and so on.

 

But if you want to continue to use your calculated table, you'll need to add more values via the ADDCOLUMNS you have.

 

 

It worked perfectly!!! Thank you mattbrice.

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.