Need DAX formula to rank and eliminate duplicatesPowerPivot DAX - Dynamic Ranking Per Group (Min Per Group)DAX Dynamic Total with Power ViewDAX Measure - Summing up values between start-end dates in the same tableDAX % of total count if measure qualifies criteriaUse DAX to calculate sales for last full month in periodFiltering DAX to another table using current row's valueSubtracting from the same column based on select filters OptionsDAX - YTD with year on columnsDax how to get the most 3 changeable itemsCreate a Measure in Power Pivot / DAX that calculates available equipment based on date ranges

Valid term from quadratic sequence?

How would I stat a creature to be immune to everything but the Magic Missile spell? (just for fun)

What method can I use to design a dungeon difficult enough that the PCs can't make it through without killing them?

What do you call someone who asks many questions?

Arrow those variables!

Apex Framework / library for consuming REST services

Personal Teleportation: From Rags to Riches

Venezuelan girlfriend wants to travel the USA to be with me. What is the process?

Why doesn't using multiple commands with a || or && conditional work?

Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?

How do I gain back my faith in my PhD degree?

If human space travel is limited by the G force vulnerability, is there a way to counter G forces?

Extract rows of a table, that include less than x NULLs

Why didn't Boeing produce its own regional jet?

Solving a recurrence relation (poker chips)

Size of subfigure fitting its content (tikzpicture)

Is "remove commented out code" correct English?

GFCI outlets - can they be repaired? Are they really needed at the end of a circuit?

Im going to France and my passport expires June 19th

How can saying a song's name be a copyright violation?

Why was the shrinking from 8″ made only to 5.25″ and not smaller (4″ or less)?

Why do bosons tend to occupy the same state?

What is the idiomatic way to say "clothing fits"?

What about the virus in 12 Monkeys?



Need DAX formula to rank and eliminate duplicates


PowerPivot DAX - Dynamic Ranking Per Group (Min Per Group)DAX Dynamic Total with Power ViewDAX Measure - Summing up values between start-end dates in the same tableDAX % of total count if measure qualifies criteriaUse DAX to calculate sales for last full month in periodFiltering DAX to another table using current row's valueSubtracting from the same column based on select filters OptionsDAX - YTD with year on columnsDax how to get the most 3 changeable itemsCreate a Measure in Power Pivot / DAX that calculates available equipment based on date ranges













0















I have a pivot table that looks like this:



enter image description here



Problem: Donors don't add up to the grand total, like the giving does. This is because a donor's age range is determined for each and every gift, based on the date of that gift, and over the course of a year a donor can cross ranges.



Request: I would like a DAX formula to limit donors to one range. In an ideal world, it would be based on which age range they fall in for the majority of the year, but I would settle for just arbitrarily keeping one of them. I believe in SQL you could go about this by partitioning and ranking.



Sample of Underlying Table:



| Donation_ID | Donor_ID | Donation_Date | Amount | age at time of gift | summary_range |
|-------------|----------|---------------|--------|---------------------|---------------|
| 1 | 100 | 3/15/2017 | 400 | 39 | <40 |
| 2 | 101 | 4/3/2017 | 50 | 69 | 60-69 |
| 3 | 100 | 5/30/2017 | 15 | 40 | 40-49 |
| 4 | 101 | 10/7/2017 | 20 | 69 | 60-69 |
| 5 | 100 | 1/23/2018 | 220 | 40 | 40-49 |
| 6 | 101 | 2/17/2018 | 25 | 70 | 70+ |




UPDATE - I got the following code to work in DaxStudio. But then it failed in Excel, saying "summarizecolumns cannot have outside filter context." Per the footnote at the bottom of the following page, it is apparently just a limitation of Excel: https://www.sqlbi.com/articles/introducing-summarizecolumns/



EVALUATE(

// filter context of the pivot table EXCEPT no filter on age range
var fc = CALCULATETABLE(
data_table,
data_table[Donation_Date] >= date(2017,3,1),
data_table[Donation_Date] <= date(2018,2,28)
)

var hh = SUMMARIZECOLUMNS(data_table[Donor_ID], data_table[summary_range],data_table[age at time of gift], fc)

var ranked =
ADDCOLUMNS(
hh,
"RankByAge",
RANKX (
FILTER(
SUMMARIZECOLUMNS(
data_table[Donor_ID],data_table[age at time of gift],
hh
),
data_table[Donor_ID] = EARLIER(data_table[Donor_ID])
),
data_table[age at time of gift],
,
desc,
DENSE
)
)

return

// ultimately need to count the rows rather than just return them
// the second criteria would come from the filter context in Excel
FILTER(ranked, [RankByAge] = 1 && [summary_range] = "<40" )

)









share|improve this question
























  • You are more likely to get help if you provide a more descriptive title and include a sample of what your data table looks like. An mcve would be ideal.

    – Alexis Olson
    Mar 9 at 23:59






  • 2





    Thanks @AlexisOlson. I was frustrated by the down votes, but maybe that explains it. I've edited the question to provide a different title and sample data.

    – Roger
    Mar 10 at 17:30






  • 1





    It's always hard figuring norms in a new community. Your edit makes this a much better question!

    – Alexis Olson
    Mar 10 at 21:13















0















I have a pivot table that looks like this:



enter image description here



Problem: Donors don't add up to the grand total, like the giving does. This is because a donor's age range is determined for each and every gift, based on the date of that gift, and over the course of a year a donor can cross ranges.



Request: I would like a DAX formula to limit donors to one range. In an ideal world, it would be based on which age range they fall in for the majority of the year, but I would settle for just arbitrarily keeping one of them. I believe in SQL you could go about this by partitioning and ranking.



Sample of Underlying Table:



| Donation_ID | Donor_ID | Donation_Date | Amount | age at time of gift | summary_range |
|-------------|----------|---------------|--------|---------------------|---------------|
| 1 | 100 | 3/15/2017 | 400 | 39 | <40 |
| 2 | 101 | 4/3/2017 | 50 | 69 | 60-69 |
| 3 | 100 | 5/30/2017 | 15 | 40 | 40-49 |
| 4 | 101 | 10/7/2017 | 20 | 69 | 60-69 |
| 5 | 100 | 1/23/2018 | 220 | 40 | 40-49 |
| 6 | 101 | 2/17/2018 | 25 | 70 | 70+ |




UPDATE - I got the following code to work in DaxStudio. But then it failed in Excel, saying "summarizecolumns cannot have outside filter context." Per the footnote at the bottom of the following page, it is apparently just a limitation of Excel: https://www.sqlbi.com/articles/introducing-summarizecolumns/



EVALUATE(

// filter context of the pivot table EXCEPT no filter on age range
var fc = CALCULATETABLE(
data_table,
data_table[Donation_Date] >= date(2017,3,1),
data_table[Donation_Date] <= date(2018,2,28)
)

var hh = SUMMARIZECOLUMNS(data_table[Donor_ID], data_table[summary_range],data_table[age at time of gift], fc)

var ranked =
ADDCOLUMNS(
hh,
"RankByAge",
RANKX (
FILTER(
SUMMARIZECOLUMNS(
data_table[Donor_ID],data_table[age at time of gift],
hh
),
data_table[Donor_ID] = EARLIER(data_table[Donor_ID])
),
data_table[age at time of gift],
,
desc,
DENSE
)
)

return

// ultimately need to count the rows rather than just return them
// the second criteria would come from the filter context in Excel
FILTER(ranked, [RankByAge] = 1 && [summary_range] = "<40" )

)









share|improve this question
























  • You are more likely to get help if you provide a more descriptive title and include a sample of what your data table looks like. An mcve would be ideal.

    – Alexis Olson
    Mar 9 at 23:59






  • 2





    Thanks @AlexisOlson. I was frustrated by the down votes, but maybe that explains it. I've edited the question to provide a different title and sample data.

    – Roger
    Mar 10 at 17:30






  • 1





    It's always hard figuring norms in a new community. Your edit makes this a much better question!

    – Alexis Olson
    Mar 10 at 21:13













0












0








0








I have a pivot table that looks like this:



enter image description here



Problem: Donors don't add up to the grand total, like the giving does. This is because a donor's age range is determined for each and every gift, based on the date of that gift, and over the course of a year a donor can cross ranges.



Request: I would like a DAX formula to limit donors to one range. In an ideal world, it would be based on which age range they fall in for the majority of the year, but I would settle for just arbitrarily keeping one of them. I believe in SQL you could go about this by partitioning and ranking.



Sample of Underlying Table:



| Donation_ID | Donor_ID | Donation_Date | Amount | age at time of gift | summary_range |
|-------------|----------|---------------|--------|---------------------|---------------|
| 1 | 100 | 3/15/2017 | 400 | 39 | <40 |
| 2 | 101 | 4/3/2017 | 50 | 69 | 60-69 |
| 3 | 100 | 5/30/2017 | 15 | 40 | 40-49 |
| 4 | 101 | 10/7/2017 | 20 | 69 | 60-69 |
| 5 | 100 | 1/23/2018 | 220 | 40 | 40-49 |
| 6 | 101 | 2/17/2018 | 25 | 70 | 70+ |




UPDATE - I got the following code to work in DaxStudio. But then it failed in Excel, saying "summarizecolumns cannot have outside filter context." Per the footnote at the bottom of the following page, it is apparently just a limitation of Excel: https://www.sqlbi.com/articles/introducing-summarizecolumns/



EVALUATE(

// filter context of the pivot table EXCEPT no filter on age range
var fc = CALCULATETABLE(
data_table,
data_table[Donation_Date] >= date(2017,3,1),
data_table[Donation_Date] <= date(2018,2,28)
)

var hh = SUMMARIZECOLUMNS(data_table[Donor_ID], data_table[summary_range],data_table[age at time of gift], fc)

var ranked =
ADDCOLUMNS(
hh,
"RankByAge",
RANKX (
FILTER(
SUMMARIZECOLUMNS(
data_table[Donor_ID],data_table[age at time of gift],
hh
),
data_table[Donor_ID] = EARLIER(data_table[Donor_ID])
),
data_table[age at time of gift],
,
desc,
DENSE
)
)

return

// ultimately need to count the rows rather than just return them
// the second criteria would come from the filter context in Excel
FILTER(ranked, [RankByAge] = 1 && [summary_range] = "<40" )

)









share|improve this question
















I have a pivot table that looks like this:



enter image description here



Problem: Donors don't add up to the grand total, like the giving does. This is because a donor's age range is determined for each and every gift, based on the date of that gift, and over the course of a year a donor can cross ranges.



Request: I would like a DAX formula to limit donors to one range. In an ideal world, it would be based on which age range they fall in for the majority of the year, but I would settle for just arbitrarily keeping one of them. I believe in SQL you could go about this by partitioning and ranking.



Sample of Underlying Table:



| Donation_ID | Donor_ID | Donation_Date | Amount | age at time of gift | summary_range |
|-------------|----------|---------------|--------|---------------------|---------------|
| 1 | 100 | 3/15/2017 | 400 | 39 | <40 |
| 2 | 101 | 4/3/2017 | 50 | 69 | 60-69 |
| 3 | 100 | 5/30/2017 | 15 | 40 | 40-49 |
| 4 | 101 | 10/7/2017 | 20 | 69 | 60-69 |
| 5 | 100 | 1/23/2018 | 220 | 40 | 40-49 |
| 6 | 101 | 2/17/2018 | 25 | 70 | 70+ |




UPDATE - I got the following code to work in DaxStudio. But then it failed in Excel, saying "summarizecolumns cannot have outside filter context." Per the footnote at the bottom of the following page, it is apparently just a limitation of Excel: https://www.sqlbi.com/articles/introducing-summarizecolumns/



EVALUATE(

// filter context of the pivot table EXCEPT no filter on age range
var fc = CALCULATETABLE(
data_table,
data_table[Donation_Date] >= date(2017,3,1),
data_table[Donation_Date] <= date(2018,2,28)
)

var hh = SUMMARIZECOLUMNS(data_table[Donor_ID], data_table[summary_range],data_table[age at time of gift], fc)

var ranked =
ADDCOLUMNS(
hh,
"RankByAge",
RANKX (
FILTER(
SUMMARIZECOLUMNS(
data_table[Donor_ID],data_table[age at time of gift],
hh
),
data_table[Donor_ID] = EARLIER(data_table[Donor_ID])
),
data_table[age at time of gift],
,
desc,
DENSE
)
)

return

// ultimately need to count the rows rather than just return them
// the second criteria would come from the filter context in Excel
FILTER(ranked, [RankByAge] = 1 && [summary_range] = "<40" )

)






dax powerpivot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 10 at 17:28







Roger

















asked Mar 8 at 22:14









RogerRoger

186




186












  • You are more likely to get help if you provide a more descriptive title and include a sample of what your data table looks like. An mcve would be ideal.

    – Alexis Olson
    Mar 9 at 23:59






  • 2





    Thanks @AlexisOlson. I was frustrated by the down votes, but maybe that explains it. I've edited the question to provide a different title and sample data.

    – Roger
    Mar 10 at 17:30






  • 1





    It's always hard figuring norms in a new community. Your edit makes this a much better question!

    – Alexis Olson
    Mar 10 at 21:13

















  • You are more likely to get help if you provide a more descriptive title and include a sample of what your data table looks like. An mcve would be ideal.

    – Alexis Olson
    Mar 9 at 23:59






  • 2





    Thanks @AlexisOlson. I was frustrated by the down votes, but maybe that explains it. I've edited the question to provide a different title and sample data.

    – Roger
    Mar 10 at 17:30






  • 1





    It's always hard figuring norms in a new community. Your edit makes this a much better question!

    – Alexis Olson
    Mar 10 at 21:13
















You are more likely to get help if you provide a more descriptive title and include a sample of what your data table looks like. An mcve would be ideal.

– Alexis Olson
Mar 9 at 23:59





You are more likely to get help if you provide a more descriptive title and include a sample of what your data table looks like. An mcve would be ideal.

– Alexis Olson
Mar 9 at 23:59




2




2





Thanks @AlexisOlson. I was frustrated by the down votes, but maybe that explains it. I've edited the question to provide a different title and sample data.

– Roger
Mar 10 at 17:30





Thanks @AlexisOlson. I was frustrated by the down votes, but maybe that explains it. I've edited the question to provide a different title and sample data.

– Roger
Mar 10 at 17:30




1




1





It's always hard figuring norms in a new community. Your edit makes this a much better question!

– Alexis Olson
Mar 10 at 21:13





It's always hard figuring norms in a new community. Your edit makes this a much better question!

– Alexis Olson
Mar 10 at 21:13












1 Answer
1






active

oldest

votes


















1














I think the simplest approach would be to create a couple of calculated columns to use instead.



Let's create a unique age for each donor by picking their age for their latest donation.



MaxAge =
CALCULATE(
MAX(data_table[ageattimeofgift]),
ALLEXCEPT(data_table, data_table[Donor_ID])
)


And then look up the range associated with this age.



MaxRange =
LOOKUPVALUE(
data_table[summary_range],
data_table[ageattimeofgift],
data_table[MaxAge]
)


Use this instead of summary_range in your pivot table.



(Note: You can partition the MaxAge by fiscal year as well if you like so that a donor can move between groups when viewing years separately.)




A more dynamic approach would be to use a measure which can read in filter context.



Distinct Donors =
VAR CurrentRange =
VALUES ( data_table[summary_range] )
VAR Summary =
SUMMARIZE (
ALLSELECTED ( data_table ),
data_table[Donor_ID],
"MaxAge", MAX ( data_table[ageattimeofgift] ),
"Amount", SUM ( data_table[Amount] )
)
VAR MaxRange =
ADDCOLUMNS (
Summary,
"MaxRange",
LOOKUPVALUE (
data_table[summary_range],
data_table[ageattimeofgift], [MaxAge]
)
)
RETURN
COUNTROWS(
FILTER(
MaxRange,
CONTAINS(
CurrentRange,
[summary_range],
[MaxRange]
)
)


)



Notice that I used SUMMARIZE instead of SUMMARIZECOLUMNS. Please refer to this article for more information on the differences and limitations of these functions in different environments.



You can use the following after the RETURN instead to get the corresponding amount given.



SUMX ( FILTER ( MaxRange, [MaxRange] IN CurrentRange ), [Amount] )


Note: This IN syntax above is a newer feature. Use the CONTAINS function for backward compatibility.






share|improve this answer

























  • Wow, thanks Alexis! Your second, dynamic approach is definitely what I was trying to accomplish. I have a small problem: it doesn't like the IN syntax. Running check formula fails with the code as is. I can get it to work for the pivot table pictured above by using ` [MaxRange] = TOPN(1,CurrentRange)` instead. But that obviously fails if you rearrange the pivot and take the ranges off. Any thoughts?

    – Roger
    Mar 11 at 4:05











  • Ah, the IN syntax is a newer DAX feature that Excel doesn't have yet. You can use the CONTAINS function instead. Here's a useful related article: sqlbi.com/articles/the-in-operator-in-dax

    – Alexis Olson
    Mar 11 at 13:40











  • Alexis yep, using CONTAINS works. Excellent!

    – Roger
    Mar 11 at 20:56











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071734%2fneed-dax-formula-to-rank-and-eliminate-duplicates%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I think the simplest approach would be to create a couple of calculated columns to use instead.



Let's create a unique age for each donor by picking their age for their latest donation.



MaxAge =
CALCULATE(
MAX(data_table[ageattimeofgift]),
ALLEXCEPT(data_table, data_table[Donor_ID])
)


And then look up the range associated with this age.



MaxRange =
LOOKUPVALUE(
data_table[summary_range],
data_table[ageattimeofgift],
data_table[MaxAge]
)


Use this instead of summary_range in your pivot table.



(Note: You can partition the MaxAge by fiscal year as well if you like so that a donor can move between groups when viewing years separately.)




A more dynamic approach would be to use a measure which can read in filter context.



Distinct Donors =
VAR CurrentRange =
VALUES ( data_table[summary_range] )
VAR Summary =
SUMMARIZE (
ALLSELECTED ( data_table ),
data_table[Donor_ID],
"MaxAge", MAX ( data_table[ageattimeofgift] ),
"Amount", SUM ( data_table[Amount] )
)
VAR MaxRange =
ADDCOLUMNS (
Summary,
"MaxRange",
LOOKUPVALUE (
data_table[summary_range],
data_table[ageattimeofgift], [MaxAge]
)
)
RETURN
COUNTROWS(
FILTER(
MaxRange,
CONTAINS(
CurrentRange,
[summary_range],
[MaxRange]
)
)


)



Notice that I used SUMMARIZE instead of SUMMARIZECOLUMNS. Please refer to this article for more information on the differences and limitations of these functions in different environments.



You can use the following after the RETURN instead to get the corresponding amount given.



SUMX ( FILTER ( MaxRange, [MaxRange] IN CurrentRange ), [Amount] )


Note: This IN syntax above is a newer feature. Use the CONTAINS function for backward compatibility.






share|improve this answer

























  • Wow, thanks Alexis! Your second, dynamic approach is definitely what I was trying to accomplish. I have a small problem: it doesn't like the IN syntax. Running check formula fails with the code as is. I can get it to work for the pivot table pictured above by using ` [MaxRange] = TOPN(1,CurrentRange)` instead. But that obviously fails if you rearrange the pivot and take the ranges off. Any thoughts?

    – Roger
    Mar 11 at 4:05











  • Ah, the IN syntax is a newer DAX feature that Excel doesn't have yet. You can use the CONTAINS function instead. Here's a useful related article: sqlbi.com/articles/the-in-operator-in-dax

    – Alexis Olson
    Mar 11 at 13:40











  • Alexis yep, using CONTAINS works. Excellent!

    – Roger
    Mar 11 at 20:56















1














I think the simplest approach would be to create a couple of calculated columns to use instead.



Let's create a unique age for each donor by picking their age for their latest donation.



MaxAge =
CALCULATE(
MAX(data_table[ageattimeofgift]),
ALLEXCEPT(data_table, data_table[Donor_ID])
)


And then look up the range associated with this age.



MaxRange =
LOOKUPVALUE(
data_table[summary_range],
data_table[ageattimeofgift],
data_table[MaxAge]
)


Use this instead of summary_range in your pivot table.



(Note: You can partition the MaxAge by fiscal year as well if you like so that a donor can move between groups when viewing years separately.)




A more dynamic approach would be to use a measure which can read in filter context.



Distinct Donors =
VAR CurrentRange =
VALUES ( data_table[summary_range] )
VAR Summary =
SUMMARIZE (
ALLSELECTED ( data_table ),
data_table[Donor_ID],
"MaxAge", MAX ( data_table[ageattimeofgift] ),
"Amount", SUM ( data_table[Amount] )
)
VAR MaxRange =
ADDCOLUMNS (
Summary,
"MaxRange",
LOOKUPVALUE (
data_table[summary_range],
data_table[ageattimeofgift], [MaxAge]
)
)
RETURN
COUNTROWS(
FILTER(
MaxRange,
CONTAINS(
CurrentRange,
[summary_range],
[MaxRange]
)
)


)



Notice that I used SUMMARIZE instead of SUMMARIZECOLUMNS. Please refer to this article for more information on the differences and limitations of these functions in different environments.



You can use the following after the RETURN instead to get the corresponding amount given.



SUMX ( FILTER ( MaxRange, [MaxRange] IN CurrentRange ), [Amount] )


Note: This IN syntax above is a newer feature. Use the CONTAINS function for backward compatibility.






share|improve this answer

























  • Wow, thanks Alexis! Your second, dynamic approach is definitely what I was trying to accomplish. I have a small problem: it doesn't like the IN syntax. Running check formula fails with the code as is. I can get it to work for the pivot table pictured above by using ` [MaxRange] = TOPN(1,CurrentRange)` instead. But that obviously fails if you rearrange the pivot and take the ranges off. Any thoughts?

    – Roger
    Mar 11 at 4:05











  • Ah, the IN syntax is a newer DAX feature that Excel doesn't have yet. You can use the CONTAINS function instead. Here's a useful related article: sqlbi.com/articles/the-in-operator-in-dax

    – Alexis Olson
    Mar 11 at 13:40











  • Alexis yep, using CONTAINS works. Excellent!

    – Roger
    Mar 11 at 20:56













1












1








1







I think the simplest approach would be to create a couple of calculated columns to use instead.



Let's create a unique age for each donor by picking their age for their latest donation.



MaxAge =
CALCULATE(
MAX(data_table[ageattimeofgift]),
ALLEXCEPT(data_table, data_table[Donor_ID])
)


And then look up the range associated with this age.



MaxRange =
LOOKUPVALUE(
data_table[summary_range],
data_table[ageattimeofgift],
data_table[MaxAge]
)


Use this instead of summary_range in your pivot table.



(Note: You can partition the MaxAge by fiscal year as well if you like so that a donor can move between groups when viewing years separately.)




A more dynamic approach would be to use a measure which can read in filter context.



Distinct Donors =
VAR CurrentRange =
VALUES ( data_table[summary_range] )
VAR Summary =
SUMMARIZE (
ALLSELECTED ( data_table ),
data_table[Donor_ID],
"MaxAge", MAX ( data_table[ageattimeofgift] ),
"Amount", SUM ( data_table[Amount] )
)
VAR MaxRange =
ADDCOLUMNS (
Summary,
"MaxRange",
LOOKUPVALUE (
data_table[summary_range],
data_table[ageattimeofgift], [MaxAge]
)
)
RETURN
COUNTROWS(
FILTER(
MaxRange,
CONTAINS(
CurrentRange,
[summary_range],
[MaxRange]
)
)


)



Notice that I used SUMMARIZE instead of SUMMARIZECOLUMNS. Please refer to this article for more information on the differences and limitations of these functions in different environments.



You can use the following after the RETURN instead to get the corresponding amount given.



SUMX ( FILTER ( MaxRange, [MaxRange] IN CurrentRange ), [Amount] )


Note: This IN syntax above is a newer feature. Use the CONTAINS function for backward compatibility.






share|improve this answer















I think the simplest approach would be to create a couple of calculated columns to use instead.



Let's create a unique age for each donor by picking their age for their latest donation.



MaxAge =
CALCULATE(
MAX(data_table[ageattimeofgift]),
ALLEXCEPT(data_table, data_table[Donor_ID])
)


And then look up the range associated with this age.



MaxRange =
LOOKUPVALUE(
data_table[summary_range],
data_table[ageattimeofgift],
data_table[MaxAge]
)


Use this instead of summary_range in your pivot table.



(Note: You can partition the MaxAge by fiscal year as well if you like so that a donor can move between groups when viewing years separately.)




A more dynamic approach would be to use a measure which can read in filter context.



Distinct Donors =
VAR CurrentRange =
VALUES ( data_table[summary_range] )
VAR Summary =
SUMMARIZE (
ALLSELECTED ( data_table ),
data_table[Donor_ID],
"MaxAge", MAX ( data_table[ageattimeofgift] ),
"Amount", SUM ( data_table[Amount] )
)
VAR MaxRange =
ADDCOLUMNS (
Summary,
"MaxRange",
LOOKUPVALUE (
data_table[summary_range],
data_table[ageattimeofgift], [MaxAge]
)
)
RETURN
COUNTROWS(
FILTER(
MaxRange,
CONTAINS(
CurrentRange,
[summary_range],
[MaxRange]
)
)


)



Notice that I used SUMMARIZE instead of SUMMARIZECOLUMNS. Please refer to this article for more information on the differences and limitations of these functions in different environments.



You can use the following after the RETURN instead to get the corresponding amount given.



SUMX ( FILTER ( MaxRange, [MaxRange] IN CurrentRange ), [Amount] )


Note: This IN syntax above is a newer feature. Use the CONTAINS function for backward compatibility.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 11 at 21:12

























answered Mar 10 at 21:13









Alexis OlsonAlexis Olson

15.2k22036




15.2k22036












  • Wow, thanks Alexis! Your second, dynamic approach is definitely what I was trying to accomplish. I have a small problem: it doesn't like the IN syntax. Running check formula fails with the code as is. I can get it to work for the pivot table pictured above by using ` [MaxRange] = TOPN(1,CurrentRange)` instead. But that obviously fails if you rearrange the pivot and take the ranges off. Any thoughts?

    – Roger
    Mar 11 at 4:05











  • Ah, the IN syntax is a newer DAX feature that Excel doesn't have yet. You can use the CONTAINS function instead. Here's a useful related article: sqlbi.com/articles/the-in-operator-in-dax

    – Alexis Olson
    Mar 11 at 13:40











  • Alexis yep, using CONTAINS works. Excellent!

    – Roger
    Mar 11 at 20:56

















  • Wow, thanks Alexis! Your second, dynamic approach is definitely what I was trying to accomplish. I have a small problem: it doesn't like the IN syntax. Running check formula fails with the code as is. I can get it to work for the pivot table pictured above by using ` [MaxRange] = TOPN(1,CurrentRange)` instead. But that obviously fails if you rearrange the pivot and take the ranges off. Any thoughts?

    – Roger
    Mar 11 at 4:05











  • Ah, the IN syntax is a newer DAX feature that Excel doesn't have yet. You can use the CONTAINS function instead. Here's a useful related article: sqlbi.com/articles/the-in-operator-in-dax

    – Alexis Olson
    Mar 11 at 13:40











  • Alexis yep, using CONTAINS works. Excellent!

    – Roger
    Mar 11 at 20:56
















Wow, thanks Alexis! Your second, dynamic approach is definitely what I was trying to accomplish. I have a small problem: it doesn't like the IN syntax. Running check formula fails with the code as is. I can get it to work for the pivot table pictured above by using ` [MaxRange] = TOPN(1,CurrentRange)` instead. But that obviously fails if you rearrange the pivot and take the ranges off. Any thoughts?

– Roger
Mar 11 at 4:05





Wow, thanks Alexis! Your second, dynamic approach is definitely what I was trying to accomplish. I have a small problem: it doesn't like the IN syntax. Running check formula fails with the code as is. I can get it to work for the pivot table pictured above by using ` [MaxRange] = TOPN(1,CurrentRange)` instead. But that obviously fails if you rearrange the pivot and take the ranges off. Any thoughts?

– Roger
Mar 11 at 4:05













Ah, the IN syntax is a newer DAX feature that Excel doesn't have yet. You can use the CONTAINS function instead. Here's a useful related article: sqlbi.com/articles/the-in-operator-in-dax

– Alexis Olson
Mar 11 at 13:40





Ah, the IN syntax is a newer DAX feature that Excel doesn't have yet. You can use the CONTAINS function instead. Here's a useful related article: sqlbi.com/articles/the-in-operator-in-dax

– Alexis Olson
Mar 11 at 13:40













Alexis yep, using CONTAINS works. Excellent!

– Roger
Mar 11 at 20:56





Alexis yep, using CONTAINS works. Excellent!

– Roger
Mar 11 at 20:56



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071734%2fneed-dax-formula-to-rank-and-eliminate-duplicates%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme