Find rows relative to current row's value in excel (DAX)

前提是你 提交于 2019-12-04 04:52:32

问题


Is there a way to filter rows based on your current row's value using DAX (I'm using power pivot) ?

In other words, If I had a table "progress" with 'ID' that is incremented in each row, and a 'Percentage' columns and another table containing

I want to create a column called old percentage = percentage of (progress[ID] -1)

Is this possible in excel ?

I couldn't find any straightforward command in this, and excuse me if there were, I am still new to power pivot.

My way of doing it is perhaps to create a new column old ID = progress[ID] -1 then create a new table in power pivot which is a duplicate of the current table but then I link it with Old ID instead of the current ID. Then in the end I do a old percentage column = RELATED([percentage]);

Is this a valid approach towards the problem ? And can this be further optimized ?

Thank you.

EDIT: I've added an image to help display what I need

)

回答1:


JShmay,

the solution could be much simpler if you use combination of SUMX function and EARLIER.

Simply add a new calculated column with this formula:

=SUMX (
    FILTER ( Progress, EARLIER ( [ID] ) = [ID] + 1 ),
    Progress[PERCENTAGE]
)

EARLIER returns the current row, so if you compare it with the previous one, it can then return the correct value. This approach might not be very intuitive, but it's much more efficient and could save you lot of time.

The output should then look like this:

Try playing around to achieve the desired result, but I believe this is the simplest and most efficient approach (as far as processing time goes).

Hope this helps.

UPDATE: Read this article about using EARLIER in DAX queries. It might be helpful.




回答2:


An alternative to Petr's elegant solution is to approach this using a measure.

Which is best in this case will depend on the specifics of your model, the size of your table, how often you will use this calculation and how many times you will want to replicate it (i.e. if you wanted to add 10 calc columns, that would be a bad idea).

I like the measure approach because it uses a very re-usable pattern:

=
SUMX (
    VALUES ( Progress[ID] ),
    CALCULATE (
        SUM ( Progress[PERCENTAGE] ),
        FILTER (
            ALL ( Progress ),
            Progress[ID]
                = MAX ( Progress[ID] ) - 1
        )
    )
)

Be careful of totals using this method - turning them off is probably the 'right' thing to do.

Formula prettified using Dax Formatter




回答3:


The answer is yes. However, I'd need to see an exact sample of what you have. It sounds like you've got two columns of data and want a third that gives you some sort of answer based upon the interaction with the first two. Based upon what you're describing, a pivot table seems unnecessary so I wouldn't waste your time with one.



来源:https://stackoverflow.com/questions/26973744/find-rows-relative-to-current-rows-value-in-excel-dax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!