Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Pan_Forex
Helper III
Helper III

data modeling for chart history

Hello, I would like to edit and later export the data in csv, which will allow to do an effect like in this video: https://www.youtube.com/watch?v=4-2nqd6-ZXg

I created a new table with a list of unique dates from to. In practice, I would like each [userhash] to have a [score] value on each day even if it was not present on that day(you can assign the last value). [Score] is the average of all previous days. 

Small sample:

Date                User_hash               Score
2023-01-01uyuytrtryfgh7
2023-01-01asdasdqwecx6
2023-01-02asdasdqwecx8
2023-01-03asdasdqwecx5
2023-01-04asdasdqwecx9
2023-01-05asdasdqwecx11
2023-01-06asdasdqwecx8
2023-01-07asdasdqwecx1
2023-01-08asdasdqwecx2
2023-01-09uyuytrtryfgh9
2023-01-09asdasdqwecx10
2023-01-10juurtgfdvw4

 

Output:

Date                    User_hash               Score_hist
2023-01-01uyuytrtryfgh7
2023-01-01asdasdqwecx6
2023-01-02uyuytrtryfgh7
2023-01-02asdasdqwecx7
2023-01-03uyuytrtryfgh7
2023-01-03asdasdqwecx6.33
2023-01-04uyuytrtryfgh7
2023-01-04asdasdqwecx7
2023-01-05uyuytrtryfgh7
2023-01-05asdasdqwecx7.8
2023-01-06uyuytrtryfgh7
2023-01-06asdasdqwecx7.83
2023-01-07uyuytrtryfgh7
2023-01-07asdasdqwecx6.86
2023-01-08uyuytrtryfgh7
2023-01-08asdasdqwecx6.25
2023-01-09uyuytrtryfgh8
2023-01-09asdasdqwecx6.66
2023-01-10asdasdqwecx6.66
2023-01-10uyuytrtryfgh8
2023-01-10juurtgfdvw4
1 ACCEPTED SOLUTION
123abc
Community Champion
Community Champion

To achieve the behavior where the score from the previous day is displayed when there is no score available for a player on a particular day, you can adjust your DAX measure accordingly. You'll need to find the last available score for each player and propagate it forward.

Here's how you can modify your Score_hist measure to achieve this:

 

Score_hist =
VAR CurrentDate = SELECTEDVALUE('public random_players'[Data_random])
VAR CurrentUserHash = SELECTEDVALUE('public random_players'[user_nickname])

-- Find the last available score for the current user hash before or on the current date
VAR LastScore =
CALCULATE(
MAX('public random_players'[Score_date_player]),
FILTER(
ALL('public random_players'),
'public random_players'[Data_random] <= CurrentDate &&
'public random_players'[user_nickname] = CurrentUserHash &&
'public random_players'[Score_date_player] <> BLANK()
)
)

-- If no last score is found, return BLANK; otherwise, return the last score
RETURN
IF(
ISBLANK(LastScore),
BLANK(),
LastScore
)

 

 

In this measure:

  • We find the last available score for the current user hash before or on the current date using the LastScore variable.
  • Then, we use an IF statement to check if a last score is found. If a last score is found, we return it; otherwise, we return BLANK.

This measure should provide the behavior you described, where if a player received a score on a previous day, that score will be assigned to the player for subsequent days until a new score is recorded.

 
 
 
 

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

 

In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

View solution in original post

8 REPLIES 8
Pan_Forex
Helper III
Helper III

I've managed to do it this way so far(I assigned all players to each date(I omitted all conditions to make it simpler for me): 

New_Table =
VAR AllCombinations =
    CROSSJOIN(
        DISTINCT('public random_players'[Data_random]),
        DISTINCT('public random_players'[user_nickname])
    )
VAR ActualData =
    SELECTCOLUMNS(
        FILTER(
            'public random_players',
            'public random_players'[Data_random] = SELECTCOLUMNS(AllCombinations, "Date", [Data_random]) &&
            'public random_players'[user_nickname] = SELECTCOLUMNS(AllCombinations, "Name", [user_nickname])
        ),
        "Date", 'public random_players'[Data_random],
        "Name", 'public random_players'[user_nickname]
    )
RETURN
    SELECTCOLUMNS(
        AllCombinations,
        "Date", [Data_random],
        "Name", [user_nickname]
    )

In the new [Value] column, I calculated the score for each player with the date condition. [score_date_player] is the average in the public random_players table, which calculates the average of the previous days only. 

Value =
IF(
ISBLANK(
LOOKUPVALUE(
'public random_players'[Score_date_player],
'public random_players'[user_nickname],
NowaTabela[Name]
)
),
CALCULATE(
MAX('public random_players'[Score_date_player]),
FILTER(
'public random_players',
'public random_players'[user_nickname] = NowaTabela[Name] &&
'public random_players'[data_random] < EARLIER('NowaTabela'[Date])
)
),
LOOKUPVALUE(
'public random_players'[Score_date_player],
'public random_players'[user_nickname],
NowaTabela[Name]
)
)
 
 

The problem is that Value returns correct values and a BLANK values if there is no data for specific date. How do I edit my function to return the last seen score_date_player instead of blank?

123abc
Community Champion
Community Champion

To achieve the desired outcome in Power BI using DAX, you can follow these steps:

  1. Import your data into Power BI as a table.
  2. Create a new table to generate a list of unique dates.
  3. Create relationships between your original table and the date table based on the date field.
  4. Use DAX measures to calculate the average score for each user_hash up to the current date.

Here's how you can write the DAX measures:

  1. Calculate the cumulative sum of scores for each user_hash:

CumulativeScore =
VAR CurrentDate = MAX('Date'[Date])
RETURN
CALCULATE(
AVERAGE('YourTable'[Score]),
FILTER(
ALL('Date'),
'Date'[Date] <= CurrentDate
)
)

 

Use the CumulativeScore measure to populate the Score_hist column in your final table visualization:

 

Score_hist =
VAR CurrentUserHash = 'YourTable'[User_hash]
VAR CurrentDate = MAX('Date'[Date])
RETURN
CALCULATE(
[CumulativeScore],
FILTER(
ALL('YourTable'),
'YourTable'[User_hash] = CurrentUserHash &&
'YourTable'[Date] <= CurrentDate
)
)

 

Once you have created these measures, you can create a new table visualization in Power BI. Place the Date column from your date table and the User_hash column from your original table into the Rows field, and then add the Score_hist measure to the Values field.

This should give you a table with the desired output, where each user_hash has a score value for each day, even if they were not present on that day. The score is the average of all previous days.

 

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

 

In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

Thank you very much for your help. I did everything as instructed but I have a problem with the Score_hist measure. I can't denominate CurrentUserHash and refer to my main table in it. 

123abc
Community Champion
Community Champion

I see. It seems like you want to calculate the historical average score for each user_hash up to the current date. Let me adjust the measure to include the reference to the current user_hash in the main table. Here's how you can modify the measure:

 

Score_hist =
VAR CurrentDate = MAX('DateTable'[Date])
VAR CurrentUserHash = SELECTEDVALUE('OriginalData'[User_hash])
RETURN
CALCULATE(
AVERAGE('OriginalData'[Score]),
FILTER(
ALL('OriginalData'),
'OriginalData'[Date] <= CurrentDate &&
'OriginalData'[User_hash] = CurrentUserHash
)
)

 

This measure calculates the historical average score for the currently selected user_hash up to the current date. It filters the OriginalData table to include only rows where the Date is less than or equal to the current date and where the User_hash matches the selected user_hash.

Make sure to replace 'OriginalData' with the name of your main table if it's different.

You can then use this measure in your Power BI visualizations alongside the Date and User_hash fields to display the historical average score for each user_hash on each date.

 

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

 

In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

Thank you for your reply. Now the measure works almost correctly. The problem is that there are days when there is no player data. If I create an animated chart then the given player will disappear from the chart on days when he is not there.

123abc
Community Champion
Community Champion

To ensure that the players remain visible on days when there is no data for them, you need to make sure that your data model includes all the players for all dates, even if they don't have any associated data for certain days. This means you need to ensure that your calendar table includes all dates and is properly related to your main data table.

Here are the steps you can take to ensure the players remain visible on the chart:

  1. Calendar Table: Make sure your calendar table includes all the dates you want to visualize on your chart. This table should cover a range of dates that includes the earliest date in your data and the latest date you want to display.

  2. Relationships: Ensure there is a relationship between your calendar table and your main data table. This relationship should be based on the date column present in both tables.

  3. Update DAX Calculation: Adjust your DAX calculation to consider all dates and all players, even if there is no data available for certain combinations of dates and players.

Here's an updated version of the DAX calculation for Score_hist that considers all dates and all players:

 

Score_hist =
VAR CurrentDate = 'Calendar'[Date]
VAR CurrentUserHash = 'YourTable'[User_hash]

RETURN
IF (
ISBLANK ( CurrentUserHash ),
BLANK (),
CALCULATE (
AVERAGE ( 'YourTable'[Score] ),
FILTER (
'YourTable',
'YourTable'[Date] <= CurrentDate &&
'YourTable'[User_hash] = CurrentUserHash
)
)
)

 

This modification ensures that even if there's no data available for a particular player on a certain date, the player will still be included in the visualization with a blank score value. This way, the player will remain visible on the chart throughout the date range you're visualizing.

Make sure to adjust the DAX expression according to your actual table and column names in Power BI. With these adjustments, your animated chart should display all players consistently across all dates.

 

If I answered your question, please mark my post as solution, Appreciate your Kudos.

 

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

Thank you for your reply. Everything is correct except that instead of BLANK score it should display the score from the previous day when the player last received a score. If someone received a score on day 1 and, for example, 9 then on days 2-8 he should be assigned a score from day 1.

123abc
Community Champion
Community Champion

To achieve the behavior where the score from the previous day is displayed when there is no score available for a player on a particular day, you can adjust your DAX measure accordingly. You'll need to find the last available score for each player and propagate it forward.

Here's how you can modify your Score_hist measure to achieve this:

 

Score_hist =
VAR CurrentDate = SELECTEDVALUE('public random_players'[Data_random])
VAR CurrentUserHash = SELECTEDVALUE('public random_players'[user_nickname])

-- Find the last available score for the current user hash before or on the current date
VAR LastScore =
CALCULATE(
MAX('public random_players'[Score_date_player]),
FILTER(
ALL('public random_players'),
'public random_players'[Data_random] <= CurrentDate &&
'public random_players'[user_nickname] = CurrentUserHash &&
'public random_players'[Score_date_player] <> BLANK()
)
)

-- If no last score is found, return BLANK; otherwise, return the last score
RETURN
IF(
ISBLANK(LastScore),
BLANK(),
LastScore
)

 

 

In this measure:

  • We find the last available score for the current user hash before or on the current date using the LastScore variable.
  • Then, we use an IF statement to check if a last score is found. If a last score is found, we return it; otherwise, we return BLANK.

This measure should provide the behavior you described, where if a player received a score on a previous day, that score will be assigned to the player for subsequent days until a new score is recorded.

 
 
 
 

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

 

In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors