Forum Discussion

Dimi_2207's avatar
Dimi_2207
Icon for Helper I rankHelper I
2 years ago
Solved

To show previous value based another column in DAX

Hello !

 

I have a big set of data and need to show in the column "Previous agent" the agent who took the previous call from the same phone number, please see the example below:

 

DatePhone numberAgentCall AttemptPrevious Agent
13-12-23 16:0211111Agent 11 
14-12-23 16:0311111Agent 22Agent 1
27-12-23 13:4911111Agent 33Agent 2
28-12-23 13:1822222Agent 31 
29-12-23 11:4922222Agent 22Agent 3
29-12-23 12:4933333Agent 2 1 
29-12-23 15:4233333Agent 12Agent 2

 

I've found solution for this, it works, but it is very slow... Almost impossible to work when i have big volumes of data (currently i have 34 000 rows).

Here is the solution i've found: 

 

 

Could you, please, help me with alternative in DAX that is faster ?

 

Thank you very much !

  • Hi,

    Write this calculated column formula

    Column = LOOKUPVALUE(Data[Agent],Data[Date],CALCULATE(MAX(Data[Date]),FILTER(Data,Data[Phone]=EARLIER(Data[Phone])&&Data[Date]<EARLIER(Data[Date]))),Data[Phone],Data[Phone])

    Hope this helps.

     

6 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    Dimi_2207 Try a calculated column perhaps? Generally looks like MTBF without the final step, you just want the previous value, not a difference or any calculation. See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/339586.
    The basic pattern is:
    Column = 
      VAR __Current = [Value]
      VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

      VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
    RETURN
      __Current - __Previous

  • Hi,

    This should ideally be solved with a calculated column formula

    Previous agent = CALCULATE(MAX(Data[Agent]),FILTER(Data,Data[Phone number]=EARLIER(Data[Phone number])&&Data[Date]<EARLIER(Data[Date])))

    Hope this helps.

     

    • Dimi_2207's avatar
      Dimi_2207
      Icon for Helper I rankHelper I

      Thank you very much, it works great and quickly with the initial sample i've provided, where i have: Agent 1, Agent 2, Agent 3...

      Actually, i have names in that column (sorry, i had to be more precise from the start!)

      And, in this case, this does not work...Please, see, updated sample size.

      Expectation: 

      PhoneDateAgentPrevious agent
      11108-11-23 08:24Kate 
      11108-11-23 09:48AlexKate
      11108-11-23 10:38ZaraAlex
      11108-11-23 10:57MichaelZara
      11108-11-23 14:36NatalyMichael
      11108-11-23 15:54YohanNataly
      11109-11-23 08:38SofiaYohan
      11109-11-23 10:14SofiaSofia
      11109-11-23 10:15RikSofia
      22213-12-23 16:02Rik 
      22214-12-23 16:03AmandaRik
      33322-12-23 19:25Maria 
      33323-12-23 18:54CyrilMaria
      33323-12-23 19:01IvanCyril
      22227-12-23 13:49NikolasAmanda
      33328-12-23 08:56ElenaIvan

       

      What Power BI shows:

       

      • Ashish_Mathur's avatar
        Ashish_Mathur
        Icon for Super User rankSuper User

        Hi,

        Write this calculated column formula

        Column = LOOKUPVALUE(Data[Agent],Data[Date],CALCULATE(MAX(Data[Date]),FILTER(Data,Data[Phone]=EARLIER(Data[Phone])&&Data[Date]<EARLIER(Data[Date]))),Data[Phone],Data[Phone])

        Hope this helps.