Power BI - max date or nearest date of selected date










1















Object: Sum up the nearest date's value by a given date



Here is my data



Table: MyData
+-------------------------------+
| ID TradeDate Value |
+-------------------------------+
| 1 2018/11/30 105 |
| 1 2018/11/8 101 |
| 1 2018/10/31 100 |
| 1 2018/9/30 100 |
| 2 2018/11/30 200 |
| 2 2018/10/31 201 |
| 2 2018/9/30 205 |
| 3 2018/11/30 300 |
| 3 2018/10/31 305 |
| 3 2018/9/30 301 |
+-------------------------------+


I create a table named 'DateList' and use slicer to select a specified date



DateList Slicer



I want to achieve the result as follows:



as of *11/9/2018*
+-----------------------------------+
| ID TradeDate Value |
+-----------------------------------+
| 1 2018/11/8 101 |
| 2 2018/10/31 201 |
| 3 2018/10/31 305 |
+-----------------------------------+
| Total 607 |
+-----------------------------------+


Currently, I try to use the steps to achieve the above result.



First, i want to find the nearest date from table 'MyData' use the new measure



MyMaxDate = CALCULATE(MAX(MyData[TradeDate]),Filter(MyData, MyData[TradeDate] <= FIRSTDATE(DateList[Date]) ))


Second, i create a new measure "MySum" to the sum up the values if [tradedate] equal to the "MyMaxDate"



MySum = CALCULATE(SUM(MyDate[Value]),Filter(MyData, MyData[TradeDate] = MyMaxDate))


Third, i create a matrix to show the result (see Result)



Unfortunately, the result 1313 is different from my goal 607
So, how can i fix my DAX formula to achieve the right result?



Many Thanks










share|improve this question
























  • It's not clear what you want to achieve. How does a slicer relate to the formula you post as DateList = ADDCOLUMNS(CALENDAR... ?? What is the logic IN PLAIN WORDS that if you select 11/9/2018 the result will be these three dates? Your question is not clear. Please provide more detail. Please edit your question to provide these details. Do not do that in a comment. Post a comment when you have updated your question to alert your followers.

    – teylyn
    Nov 14 '18 at 6:45












  • Hi teylyn, thanks for your replay. I've been updated my question. Is that clear?

    – AndyChu
    Nov 14 '18 at 9:18















1















Object: Sum up the nearest date's value by a given date



Here is my data



Table: MyData
+-------------------------------+
| ID TradeDate Value |
+-------------------------------+
| 1 2018/11/30 105 |
| 1 2018/11/8 101 |
| 1 2018/10/31 100 |
| 1 2018/9/30 100 |
| 2 2018/11/30 200 |
| 2 2018/10/31 201 |
| 2 2018/9/30 205 |
| 3 2018/11/30 300 |
| 3 2018/10/31 305 |
| 3 2018/9/30 301 |
+-------------------------------+


I create a table named 'DateList' and use slicer to select a specified date



DateList Slicer



I want to achieve the result as follows:



as of *11/9/2018*
+-----------------------------------+
| ID TradeDate Value |
+-----------------------------------+
| 1 2018/11/8 101 |
| 2 2018/10/31 201 |
| 3 2018/10/31 305 |
+-----------------------------------+
| Total 607 |
+-----------------------------------+


Currently, I try to use the steps to achieve the above result.



First, i want to find the nearest date from table 'MyData' use the new measure



MyMaxDate = CALCULATE(MAX(MyData[TradeDate]),Filter(MyData, MyData[TradeDate] <= FIRSTDATE(DateList[Date]) ))


Second, i create a new measure "MySum" to the sum up the values if [tradedate] equal to the "MyMaxDate"



MySum = CALCULATE(SUM(MyDate[Value]),Filter(MyData, MyData[TradeDate] = MyMaxDate))


Third, i create a matrix to show the result (see Result)



Unfortunately, the result 1313 is different from my goal 607
So, how can i fix my DAX formula to achieve the right result?



Many Thanks










share|improve this question
























  • It's not clear what you want to achieve. How does a slicer relate to the formula you post as DateList = ADDCOLUMNS(CALENDAR... ?? What is the logic IN PLAIN WORDS that if you select 11/9/2018 the result will be these three dates? Your question is not clear. Please provide more detail. Please edit your question to provide these details. Do not do that in a comment. Post a comment when you have updated your question to alert your followers.

    – teylyn
    Nov 14 '18 at 6:45












  • Hi teylyn, thanks for your replay. I've been updated my question. Is that clear?

    – AndyChu
    Nov 14 '18 at 9:18













1












1








1








Object: Sum up the nearest date's value by a given date



Here is my data



Table: MyData
+-------------------------------+
| ID TradeDate Value |
+-------------------------------+
| 1 2018/11/30 105 |
| 1 2018/11/8 101 |
| 1 2018/10/31 100 |
| 1 2018/9/30 100 |
| 2 2018/11/30 200 |
| 2 2018/10/31 201 |
| 2 2018/9/30 205 |
| 3 2018/11/30 300 |
| 3 2018/10/31 305 |
| 3 2018/9/30 301 |
+-------------------------------+


I create a table named 'DateList' and use slicer to select a specified date



DateList Slicer



I want to achieve the result as follows:



as of *11/9/2018*
+-----------------------------------+
| ID TradeDate Value |
+-----------------------------------+
| 1 2018/11/8 101 |
| 2 2018/10/31 201 |
| 3 2018/10/31 305 |
+-----------------------------------+
| Total 607 |
+-----------------------------------+


Currently, I try to use the steps to achieve the above result.



First, i want to find the nearest date from table 'MyData' use the new measure



MyMaxDate = CALCULATE(MAX(MyData[TradeDate]),Filter(MyData, MyData[TradeDate] <= FIRSTDATE(DateList[Date]) ))


Second, i create a new measure "MySum" to the sum up the values if [tradedate] equal to the "MyMaxDate"



MySum = CALCULATE(SUM(MyDate[Value]),Filter(MyData, MyData[TradeDate] = MyMaxDate))


Third, i create a matrix to show the result (see Result)



Unfortunately, the result 1313 is different from my goal 607
So, how can i fix my DAX formula to achieve the right result?



Many Thanks










share|improve this question
















Object: Sum up the nearest date's value by a given date



Here is my data



Table: MyData
+-------------------------------+
| ID TradeDate Value |
+-------------------------------+
| 1 2018/11/30 105 |
| 1 2018/11/8 101 |
| 1 2018/10/31 100 |
| 1 2018/9/30 100 |
| 2 2018/11/30 200 |
| 2 2018/10/31 201 |
| 2 2018/9/30 205 |
| 3 2018/11/30 300 |
| 3 2018/10/31 305 |
| 3 2018/9/30 301 |
+-------------------------------+


I create a table named 'DateList' and use slicer to select a specified date



DateList Slicer



I want to achieve the result as follows:



as of *11/9/2018*
+-----------------------------------+
| ID TradeDate Value |
+-----------------------------------+
| 1 2018/11/8 101 |
| 2 2018/10/31 201 |
| 3 2018/10/31 305 |
+-----------------------------------+
| Total 607 |
+-----------------------------------+


Currently, I try to use the steps to achieve the above result.



First, i want to find the nearest date from table 'MyData' use the new measure



MyMaxDate = CALCULATE(MAX(MyData[TradeDate]),Filter(MyData, MyData[TradeDate] <= FIRSTDATE(DateList[Date]) ))


Second, i create a new measure "MySum" to the sum up the values if [tradedate] equal to the "MyMaxDate"



MySum = CALCULATE(SUM(MyDate[Value]),Filter(MyData, MyData[TradeDate] = MyMaxDate))


Third, i create a matrix to show the result (see Result)



Unfortunately, the result 1313 is different from my goal 607
So, how can i fix my DAX formula to achieve the right result?



Many Thanks







powerbi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 9:16







AndyChu

















asked Nov 14 '18 at 5:20









AndyChuAndyChu

185




185












  • It's not clear what you want to achieve. How does a slicer relate to the formula you post as DateList = ADDCOLUMNS(CALENDAR... ?? What is the logic IN PLAIN WORDS that if you select 11/9/2018 the result will be these three dates? Your question is not clear. Please provide more detail. Please edit your question to provide these details. Do not do that in a comment. Post a comment when you have updated your question to alert your followers.

    – teylyn
    Nov 14 '18 at 6:45












  • Hi teylyn, thanks for your replay. I've been updated my question. Is that clear?

    – AndyChu
    Nov 14 '18 at 9:18

















  • It's not clear what you want to achieve. How does a slicer relate to the formula you post as DateList = ADDCOLUMNS(CALENDAR... ?? What is the logic IN PLAIN WORDS that if you select 11/9/2018 the result will be these three dates? Your question is not clear. Please provide more detail. Please edit your question to provide these details. Do not do that in a comment. Post a comment when you have updated your question to alert your followers.

    – teylyn
    Nov 14 '18 at 6:45












  • Hi teylyn, thanks for your replay. I've been updated my question. Is that clear?

    – AndyChu
    Nov 14 '18 at 9:18
















It's not clear what you want to achieve. How does a slicer relate to the formula you post as DateList = ADDCOLUMNS(CALENDAR... ?? What is the logic IN PLAIN WORDS that if you select 11/9/2018 the result will be these three dates? Your question is not clear. Please provide more detail. Please edit your question to provide these details. Do not do that in a comment. Post a comment when you have updated your question to alert your followers.

– teylyn
Nov 14 '18 at 6:45






It's not clear what you want to achieve. How does a slicer relate to the formula you post as DateList = ADDCOLUMNS(CALENDAR... ?? What is the logic IN PLAIN WORDS that if you select 11/9/2018 the result will be these three dates? Your question is not clear. Please provide more detail. Please edit your question to provide these details. Do not do that in a comment. Post a comment when you have updated your question to alert your followers.

– teylyn
Nov 14 '18 at 6:45














Hi teylyn, thanks for your replay. I've been updated my question. Is that clear?

– AndyChu
Nov 14 '18 at 9:18





Hi teylyn, thanks for your replay. I've been updated my question. Is that clear?

– AndyChu
Nov 14 '18 at 9:18












1 Answer
1






active

oldest

votes


















0














You can calculate the closest date by taking a min over the difference in dates and then taking the minimal date with that minimal difference.



MyDate = 
VAR SlicerDate = MIN(DateList[Date])
VAR MinDiff =
MINX(
FILTER(ALL(MyData),
MyData[ID] IN VALUES(MyData[ID])
),
ABS(SlicerDate - MyData[TradeDate]))
RETURN
MINX(
FILTER(ALL(MyData),
MyData[ID] IN VALUES(MyData[ID])
&& ABS(SlicerDate - MyData[TradeDate]) = MinDiff
),
MyData[TradeDate])


From there you can create the summing measure fairly easily:



MySum = CALCULATE(SUM(MyData[Value]), FILTER(MyData, MyData[TradeDate] = [MyDate]))


Result






share|improve this answer























  • Wow, that's great. Alexis, thanks for your help

    – AndyChu
    Nov 15 '18 at 0:51










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%2f53293637%2fpower-bi-max-date-or-nearest-date-of-selected-date%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









0














You can calculate the closest date by taking a min over the difference in dates and then taking the minimal date with that minimal difference.



MyDate = 
VAR SlicerDate = MIN(DateList[Date])
VAR MinDiff =
MINX(
FILTER(ALL(MyData),
MyData[ID] IN VALUES(MyData[ID])
),
ABS(SlicerDate - MyData[TradeDate]))
RETURN
MINX(
FILTER(ALL(MyData),
MyData[ID] IN VALUES(MyData[ID])
&& ABS(SlicerDate - MyData[TradeDate]) = MinDiff
),
MyData[TradeDate])


From there you can create the summing measure fairly easily:



MySum = CALCULATE(SUM(MyData[Value]), FILTER(MyData, MyData[TradeDate] = [MyDate]))


Result






share|improve this answer























  • Wow, that's great. Alexis, thanks for your help

    – AndyChu
    Nov 15 '18 at 0:51















0














You can calculate the closest date by taking a min over the difference in dates and then taking the minimal date with that minimal difference.



MyDate = 
VAR SlicerDate = MIN(DateList[Date])
VAR MinDiff =
MINX(
FILTER(ALL(MyData),
MyData[ID] IN VALUES(MyData[ID])
),
ABS(SlicerDate - MyData[TradeDate]))
RETURN
MINX(
FILTER(ALL(MyData),
MyData[ID] IN VALUES(MyData[ID])
&& ABS(SlicerDate - MyData[TradeDate]) = MinDiff
),
MyData[TradeDate])


From there you can create the summing measure fairly easily:



MySum = CALCULATE(SUM(MyData[Value]), FILTER(MyData, MyData[TradeDate] = [MyDate]))


Result






share|improve this answer























  • Wow, that's great. Alexis, thanks for your help

    – AndyChu
    Nov 15 '18 at 0:51













0












0








0







You can calculate the closest date by taking a min over the difference in dates and then taking the minimal date with that minimal difference.



MyDate = 
VAR SlicerDate = MIN(DateList[Date])
VAR MinDiff =
MINX(
FILTER(ALL(MyData),
MyData[ID] IN VALUES(MyData[ID])
),
ABS(SlicerDate - MyData[TradeDate]))
RETURN
MINX(
FILTER(ALL(MyData),
MyData[ID] IN VALUES(MyData[ID])
&& ABS(SlicerDate - MyData[TradeDate]) = MinDiff
),
MyData[TradeDate])


From there you can create the summing measure fairly easily:



MySum = CALCULATE(SUM(MyData[Value]), FILTER(MyData, MyData[TradeDate] = [MyDate]))


Result






share|improve this answer













You can calculate the closest date by taking a min over the difference in dates and then taking the minimal date with that minimal difference.



MyDate = 
VAR SlicerDate = MIN(DateList[Date])
VAR MinDiff =
MINX(
FILTER(ALL(MyData),
MyData[ID] IN VALUES(MyData[ID])
),
ABS(SlicerDate - MyData[TradeDate]))
RETURN
MINX(
FILTER(ALL(MyData),
MyData[ID] IN VALUES(MyData[ID])
&& ABS(SlicerDate - MyData[TradeDate]) = MinDiff
),
MyData[TradeDate])


From there you can create the summing measure fairly easily:



MySum = CALCULATE(SUM(MyData[Value]), FILTER(MyData, MyData[TradeDate] = [MyDate]))


Result







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 18:38









Alexis OlsonAlexis Olson

14.6k21834




14.6k21834












  • Wow, that's great. Alexis, thanks for your help

    – AndyChu
    Nov 15 '18 at 0:51

















  • Wow, that's great. Alexis, thanks for your help

    – AndyChu
    Nov 15 '18 at 0:51
















Wow, that's great. Alexis, thanks for your help

– AndyChu
Nov 15 '18 at 0:51





Wow, that's great. Alexis, thanks for your help

– AndyChu
Nov 15 '18 at 0:51



















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%2f53293637%2fpower-bi-max-date-or-nearest-date-of-selected-date%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

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus