Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

EARLIER function error - get count of IDs by month dynamically with slicer

Hello, I hope some help about this.

 

I managed to get the follwing graph

For this, I first created a column in ventas table with this Dax formula:

(This column is only a count of total sales or total ids in each month) 

Venta Mens. = CALCULATE (
COUNT(ventas[id]),
FILTER(ventas, FORMAT(ventas[date], "yyyymm") = FORMAT(EARLIER(ventas[date]), "yyyymm") )
)
 
Second, I use that column in this measure to get the mentioned graph:
Venta Mens. Cosecha = CALCULATE (
MAX(ventas[Venta Mens.]),
USERELATIONSHIP(ventas[date], trx_diario[dia_venta])
)

 

My problem comes because in this way, my measure is "static" and I need to be able to change it in base a slicer that have the segment column of segmentos table.

So I tried to put a single masure like this (using ALLSELECTED function), but I get error with EARLIER function (and I'm not really sure if this will give me the desire output that could be filtered with segment slicer)

Venta Mens. Cosecha =
var Venta_Mens. = CALCULATE (
COUNT(ventas[id]),
FILTER(ALLSELECTED(ventas), FORMAT(ventas[date], "yyyymm") = FORMAT(EARLIER(ventas[date]), "yyyymm") ))
return
CALCULATE (
MAX(Venta_Mens.),
USERELATIONSHIP(ventas[date], trx_diario[dia_venta])
)

 

I hope my explanation is enough and someone could give some help. Feel free to ask more details if necessary.

  • Anonymous's avatar
    Anonymous
    4 years ago

    Yes, I tried the column you suggested, I even tried that before posting

     

    But, finally I managed to get what I needed. Here the metric:

     

    Venta Mens. Cosecha =
    CALCULATE (
    DISTINCTCOUNT(ventas[id]),
    FILTER(ALLSELECTED( ventas[Trunc date]),
    ventas[Trunc date]= LASTDATE( ventas[Trunc date])
    ),
    USERELATIONSHIP(ventas[date], trx_diario[dia_venta]), ALLSELECTED(trx_diario[maduracion])
    )

6 Replies

  • Anonymous , You can use allselected and MAx in place earlier in a measure

     

    Venta Mens. Cosecha =
    var Venta_Mens. = CALCULATE (
    COUNT(ventas[id]),
    FILTER(ALLSELECTED(ventas), FORMAT(ventas[date], "yyyymm") = FORMAT(MAX(ventas[date]), "yyyymm") ))
    return
    CALCULATE (
    MAX(Venta_Mens.),
    USERELATIONSHIP(ventas[date], trx_diario[dia_venta])
    )

     

    what are you trying to do here.

     

    if you need last month data use time intelligence

    Power BI — Month on Month with or Without Time Intelligence
    https://medium.com/@amitchandak.1978/power-bi-mtd-questions-time-intelligence-3-5-64b0b4a4090e
    https://www.youtube.com/watch?v=6LUBbvcxtKA

    • Anonymous's avatar
      Anonymous
      Not applicable

      amitchandak Thanks for your suggestion.

      I have to say that searching about EARLIER function I found what you say (replacing EARLIER by MAX), but the problem it that using the formula you suggest gives me error "The MAX function only accepts a column reference as an argument." (But this function is what I need, problem is the given error)

      I don't get how to solve this error because as I understand so far, I only passing one argument to MAX function, but get error anyway.

       

  • v-kelly-msft's avatar
    v-kelly-msft
    Icon for Community Support rankCommunity Support

    Hi  Anonymous ,

     

    I would suggest you first create a column in your ventas table using below syntax:

    Year-month=FORMAT(ventas[date], "yyyymm") 

    Then create a measure as below:

    Venta Mens. Cosecha =
    var Venta_Mens. = CALCULATE (
    COUNT(ventas[id]),
    FILTER(ALLSELECTED(ventas),ventas[Year-month] = MAX(ventas[Year-month]))
    return
    CALCULATE (
    MAX(Venta_Mens.),
    USERELATIONSHIP(ventas[date], trx_diario[dia_venta])
    )

     

    Best Regards,
    Kelly

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

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello, sorry for delay, but this is the same as amitchandak answer and as I said before, I get error in MAX function (you can see it in the next image). I still don't get how to fix it.

      • v-kelly-msft's avatar
        v-kelly-msft
        Icon for Community Support rankCommunity Support

        Hi Anonymous ,

         

        Have you first created a column as I suggested before?

        Year-month=FORMAT(ventas[date], "yyyymm") 

        If you still get errors,would you pls provide some dummy data for test?

         

        Best Regards,
        Kelly

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