Forum Discussion

prashantg364's avatar
prashantg364
Helper II
2 years ago

DAX formula

CellWeek
abc35
abc34
abc33
abc32
abc31
def35
def34
def32
def31
jkl34
jkl33
jkl32
jkl31
jkl30
pqr35
pqr34
pqr33
pqr32
pqr31
xyz35
xyz34
xyz33
xyz31

 

In My data set i have 2 columns, Cell and week. Consider Week 35 as current week.
Cell abc - Appears in all weeks

Cell def - Appears in all weeks except week 33

Cell jkl - Appears in all weeks except week 35

Cell pqr - Appears in all weeks

Cell xyz - Appears in all weeks except week 32

I want my output which shows me cells which appears continously from current week till last all weeks. if any week is missing then my query end at that point for that cell.

CellWeekreason 
abc35/34/33/32/31Appears in all weeks 
def35/34Appears in all weeks except week 33, so my query should end at week 34 as there is one week gap oberved after that 
jkl since this cell didn't appear in current week, so no output expected 
pqr35/34/33/32/31Appears in all weeks 
xyz35/34/33Appears in all weeks except week 32, so my query should end at week 33 as there is one week gap oberved after that 



Pls help, how can i get this week info in one columns against each cell.
in conclusion i want all the different weeks starting current week against each cell as output and if any cell observe any week break in between then the result for that cell should be appear accordingly.

4 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    prashantg364 Here is one way, PBIX attached below signature. 

    Measure 2 = 
        VAR __MinWeek = MINX( ALL('Table'[Week]), [Week] )
        VAR __MaxWeek = MAXX( ALL('Table'[Week]), [Week] )
        VAR __MaxWeekCell = MAX( 'Table'[Week] )
        VAR __Count = COUNTROWS( DISTINCT( 'Table'[Week] ) )
        VAR __Path = CONCATENATEX( DISTINCT( 'Table'[Week] ), [Week], "|", [Week], DESC )
        VAR __Table = 
            ADDCOLUMNS(
                GENERATESERIES( 1, __Count, 1 ),
                "__Week", PATHITEM( __Path, [Value] ) + 0
            )
        VAR __Table2 = 
            ADDCOLUMNS(
                __Table,
                "__Gap", 
                    VAR __CurWeek = [__Week]
                    VAR __Next = MAXX( FILTER( __Table, [__Week] < __CurWeek ), [__Week] )
                    VAR __Result = IF( __CurWeek = __MaxWeekCell, 1, __CurWeek - __Next )
                RETURN
                    __Result
            )
        VAR __GapWeek = MAXX( FILTER( __Table2, [__Gap] > 1 ), [__Week] )
        VAR __GapWeekFinal = 
            SWITCH( TRUE(),
                __MaxWeekCell <> __MaxWeek, BLANK(),
                __GapWeek = BLANK(), __MinWeek,
                __GapWeek
            )
        VAR __Result = IF( __GapWeekFinal = BLANK(), BLANK(), CONCATENATEX( FILTER( DISTINCT( 'Table'[Week] ), [Week] >= __GapWeek ), [Week], "/", [Week], DESC ) )
    RETURN
        __Result
    • prashantg364's avatar
      prashantg364
      Helper II
      CELLMeasureMeasure 2Outputoutput expected
      ATG7130Z_7NB03_S043333correct 
      ATG8512Z_7NB03_S0433/2833/28not correct33
      ATG8524Z_7NB03_S0233/2633/26not correct33
      ATG8527Z_7NB03_S013333correct 
      ATGC004Z_7NB03_S0333/28/27/26/25/2433/28/27/26/25/24not correct33
      AYT0013H_7NB01_S0233/32/31/28/27/2433/32/31correct 
      AYT0020H_7NB01_S0133/32/31/30/29/28/27/26/25/2433/32/31/30/29/28/27/26/25/24correct 
           


      Hi Greg_Deckler 
      Thanks for the help. Majority of the cases i am getting correct output, but still some cell is giving wrong output as shown in the table above.

      Kindly re-look and help!!

       

      • Greg_Deckler's avatar
        Greg_Deckler
        Community Champion

        prashantg364 You'll have to provide a more comprehensive set of sample data then so I can find the edge cases where things are going wrong.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi prashantg364 , hello Greg_Deckler, thank you for your prompt reply!

     

    We are looking forward to your feedback if there is any update.

     

    Please remember to update this thread if you have any progress.

     

    Thank you for your understanding.