Forum Discussion
Transform and summarize data from one table into another...
- 5 years ago
Hi elietech .
The error was related with the calculation when you have a single day, I already fixed it by changing the formula to:
measure = VAR Date_Selection = MAX ( 'Dates'[Date] ) VAR Date_Selection_Next = Date_Selection + 1 VAR temp_table = FILTER ( ADDCOLUMNS ( UpDown; "Start"; DATE ( YEAR ( UpDown[DOWN_START] ); MONTH ( UpDown[DOWN_START] ); DAY ( UpDown[DOWN_START] ) ); "End"; DATE ( YEAR ( UpDown[DOWN_STOP] ); MONTH ( UpDown[DOWN_STOP] ); DAY ( UpDown[DOWN_STOP] ) ) ); [Start] <= Date_Selection && [End] >= Date_Selection ) VAR DateStart = IF ( MINX ( temp_table; UpDown[DOWN_START] ) <= Date_Selection; Date_Selection; MINX ( temp_table; UpDown[DOWN_START] ) ) VAR DateEnd = IF ( MAXX ( temp_table; UpDown[DOWN_STOP] ) >= Date_Selection_Next; Date_Selection_Next; MAXX ( temp_table; UpDown[DOWN_STOP] ) ) VAR Same_day_selection = IF ( MAXX ( temp_table; [Start] ) = MINX ( temp_table; [End] ) && MINX ( temp_table; UpDown[DOWN_START] ) >= Date_Selection; 1 ) RETURN IF ( DateEnd = BLANK (); 1440; IF ( Same_day_selection = BLANK (); DATEDIFF ( Date_Selection; DateStart; MINUTE ) + DATEDIFF ( DateEnd; Date_Selection_Next; MINUTE ); 1440 - CALCULATE ( SUM ( UpDown[DOWN_DURATION] ); FILTER ( temp_table; UpDown[DOWN_START] = DateStart ) ) ) )Check result below and attach.
Hi elietech ,
Looking at your information this is possible however you need to give some additional information on your Model.
I assume you have and equiment table that is related with your down. Create a calendar table disconnected for your filter and the add the following measure:
Measure =
VAR Date_Selection =
MAX ( 'calendar'[Date] )
VAR Date_Selection_Next = Date_Selection + 1
VAR temp_table =
FILTER (
ADDCOLUMNS (
Up_Down;
"Start";
DATE ( YEAR ( Up_Down[Down_start] ); MONTH ( Up_Down[Down_start] ); DAY ( Up_Down[Down_start] ) );
"End";
DATE ( YEAR ( Up_Down[Down_Stop] ); MONTH ( Up_Down[Down_Stop] ); DAY ( Up_Down[Down_Stop] ) )
);
[Start] <= Date_Selection
&& [End] >= Date_Selection
)
VAR DateStart =
IF (
MINX ( temp_table; Up_Down[Down_start] ) <= Date_Selection;
Date_Selection;
MINX ( temp_table; Up_Down[Down_start] )
)
VAR DateEnd =
IF (
MAXX ( temp_table; Up_Down[Down_Stop] ) >= Date_Selection_Next;
Date_Selection_Next;
MAXX ( temp_table; Up_Down[Down_Stop] )
)
VAR Same_day_selection =
IF (
MAXX ( temp_table; [Start] ) = MINX ( temp_table; [End] )
&& MINX ( temp_table; Up_Down[Down_start] ) >= Date_Selection;
1
)
RETURN
IF (
DateEnd = BLANK ();
1440;
IF (
Same_day_selection = BLANK ();
DATEDIFF ( Date_Selection; DateStart; MINUTE )
+ DATEDIFF ( DateEnd; Date_Selection_Next; MINUTE );
1440
- SUM ( Up_Down[Down_duration] ) * 1440
)
)
Check final result in PBIX file attach.
- elietech5 years ago
Helper II
Sorry for the delayed response...this looks very promising....one quick question before I start digging into it....my model already contains a "date/calendar" table...would I need to make another one? Or can I use the existing one? And yes, I do have an "Equipment" table that is relatable to the Up/Down events data....
Can't wait to give this a try...definately a level of DAX beyond what I am normally able to dream up on my own 🙂 I'm constantly amazed by the help and support I can find on this community.
- MFelix5 years ago
Super User
Hi elietech,
I use a unrelated table but the use of this other table depends on the relationship you have on your model.
How does the date table relates with the other tables in the model?
- elietech5 years ago
Helper II
Ok, so I implemented your recomended DAX code, and it appears to work, in most situations, but i'm getting some odd results of very large negative numbers, and I can't determine the cause. I mocked up a report using one month of my data, and I've attached the PBIX file...an ideas on where to start?
Guess I can't attach files...here is a link to my file in Onedrive