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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page