Forum Discussion

nicolasvargas's avatar
2 years ago

Use a variable within a filter not working properly

Hello,

 

I need to create a column which ranks a colum based on some conditions. I would like for these conditions to be included within the RANKX as a VAR instead of one by one to factorize my formula.

 

The current formula I have is this one:

 

 

 

Test1 = 
VAR Al = 0.2
VAR DBCOUNT1 = RANKX(
                    FILTER (Sheet1, Sheet1[Date] = EARLIER(Sheet1[Date]) && Sheet1[a] > Al && Sheet1[b]<0.9),
                    Sheet1[c],,ASC,DENSE)

RETURN
IF(Sheet1[a] > Al && Sheet1[b]<0.9,DBCOUNT1,-1)

 

 

 

 

 

I would like to factorize the conditions on the filter and the if at the bottom with something like this:

 

 

 

Test2 = 
VAR Al = 0.2
VAR Universe = IF(Sheet1[a] > Al && Sheet1[b]<0.9,1,0)
VAR DBCOUNT1 = RANKX(
                    FILTER (Sheet1, Sheet1[Date] = EARLIER(Sheet1[Date]) && Universe=1),
                    Sheet1[c],,ASC,DENSE)

RETURN
IF(Universe=1,DBCOUNT1,-1)

 

 

 

 

However when I apply this column Test2 outputs a wrong data. How can i do this?

This is my data and the outputs:

dateabcTest1Test2
1/1/20220.050.780.03-1-1
1/1/20220.090.930.54-1-1
1/1/20220.190.190.16-1-1
1/1/20220.330.780.5748
1/1/20220.410.120.89510
1/1/20220.440.970.44-1-1
1/1/20220.620.410.335
1/1/20220.650.630.2113
1/1/20220.740.740.2924
1/1/20220.80.980.76-1-1
2/1/20220.030.020.39-1-1
2/1/20220.060.20.59-1-1
2/1/20220.220.170.869
2/1/20220.330.280.0211
2/1/20220.570.560.91710
2/1/20220.640.50.5746
2/1/20220.690.340.0222
2/1/20220.780.530.7158
2/1/20220.820.040.1233
2/1/20220.90.960.36-1-1
3/1/20220.030.450.96-1-1
3/1/20220.190.960.38-1-1
3/1/20220.20.730.02-1-1
3/1/20220.390.640.5324
3/1/20220.430.430.6846
3/1/20220.590.620.9279
3/1/20220.670.460.4613
3/1/20220.860.410.6535
3/1/20220.880.730.7957
3/1/20220.90.60.9168

 

pd: My formula is way larger and has more conditions, this is just a sample to help solve the main issue I have

3 Replies

  • You have multiple rows per date. The Filter statement will then in some cases grab the wrong row and produce the wrong result. Note that you should use a variable for the date rather than using EARLIER.

     

    Here's a refactored version that is "good enough". 

     

     

    Test2 = 
    VAR Universe = [a] > 0.2 && [b] < 0.9
    VAR d = [date]
    VAR DBCOUNT1 = RANKX(FILTER (Sheet1, ([Date] = d) && [a] > 0.2 && [b] < 0.9),[c],,ASC,DENSE)
    RETURN IF(Universe,DBCOUNT1,-1)

     

     

    Not really different from 

    Test1 = 
    VAR d = [date]
    VAR DBCOUNT1 = RANKX(FILTER (Sheet1, [Date] = d && [a] > 0.2 && [b]<0.9),[c],,ASC,DENSE)
    RETURN IF([a] > 0.2 && [b]<0.9,DBCOUNT1,-1)

     

    • nicolasvargas's avatar
      nicolasvargas
      Helper I

      Hello, but here you're not using the "Universe" variable within the filter. And that is what I'm trying to accomplish. Any ideas?

      • lbendlin's avatar
        lbendlin
        Super User

        In this particular scenario a variable won't help as the result is different for each row.