Could you please help me on below Dax logic
I am expecting my First_Valuecolumn needs to populate based on Date,datetime and subject column.
I have tried summarize and firstnoblank dax functions but doesn't useful for my requirement.
Thanks in advance,
PS
Assuming your data looks like this
Table
+-------------+---------------------+---------+-------+
| Date | DateTime | Subject | Value |
+-------------+---------------------+---------+-------+
| 05 May 2021 | 05/05/2021 01:00:00 | b | 2500 |
+-------------+---------------------+---------+-------+
| 06 May 2021 | 05/05/2021 01:00:00 | A | 6000 |
+-------------+---------------------+---------+-------+
| 05 May 2021 | 05/05/2021 01:00:00 | A | 4500 |
+-------------+---------------------+---------+-------+
| 06 May 2021 | 05/05/2021 01:00:00 | b | 1500 |
+-------------+---------------------+---------+-------+
| 06 May 2021 | 05/05/2021 02:00:00 | A | 4100 |
+-------------+---------------------+---------+-------+
| 05 May 2021 | 05/05/2021 02:00:00 | A | 4100 |
+-------------+---------------------+---------+-------+
| 05 May 2021 | 05/05/2021 02:00:00 | b | 3500 |
+-------------+---------------------+---------+-------+
| 06 May 2021 | 05/05/2021 02:00:00 | b | 3500 |
+-------------+---------------------+---------+-------+
| 05 May 2021 | 05/05/2021 03:00:00 | A | 5500 |
+-------------+---------------------+---------+-------+
| 05 May 2021 | 05/05/2021 03:00:00 | b | 7500 |
+-------------+---------------------+---------+-------+
| 06 May 2021 | 05/05/2021 03:00:00 | A | 5500 |
+-------------+---------------------+---------+-------+
| 06 May 2021 | 05/05/2021 03:00:00 | b | 7500 |
+-------------+---------------------+---------+-------+
You can create a calculated column as the following. The idea behind this is to create variables that can capture the values of the table and use them in filter contexts.
First_Value =
VAR SubjectValue = [Subject]
VAR DateVal = [Date]
VAR MinDateTime =
CALCULATE (
MIN ( [DateTime] ),
FILTER ( 'Table', [Subject] = SubjectValue && [Date] = DateVal )
)
RETURN
SUMMARIZE (
FILTER (
'Table',
[Subject] = SubjectValue
&& [Date] = DateVal
&& [DateTime] = MinDateTime
),
[Value]
)
Related
Sample data:
+----------+------------+----------+-----------+
| PersonID | Date | Booked | Picked |
+----------+------------+----------+-----------+
| 1 | 1 Jan 2023 | 100 | 100 |
| 2 | 1 Jan 2023 | 40 | 30 |
| 3 | 1 Jan 2023 | 20 | 40 |
| 1 | 2 Jan 2023 | 50 | 80 |
| 2 | 2 Jan 2023 | 70 | 70 |
| 3 | 2 Jan 2023 | 60 | 40 |
+----------+------------+----------+-----------+
I have a measure as follows:
Performance % = DIVIDE(IF(Calls[Picked]>Calls[Booked],Calls[Booked],Called[Picked]),Calls[Booked])
I have formatted this as %
When I place this in a table visual then I get a % value.
But when I place it into a card visual then it forces me to choose sum/min/max/...
What is the way to display the value of a measure in a card visual?
How to iterate over each row to calculate the percentage value - for example - there is no DIVIDEX in dax.
Can you please try with a Measure as below-
Performance % =
DIVIDE(
IF(
SUM(Calls[Picked])>SUM(Calls[Booked]),
SUM(Calls[Booked]),
SUM(Calls[Picked])
),
SUM(Calls[Booked])
)
Could you please help me to solve the problem as I am totally new to DAX and English is not my first language so I am struggling to even find the correct question.
Here's the problem.
I have two tables:
start_balance
+------+---------------+
| Type | Start balance |
+------+---------------+
| A | 0 |
| B | 10 |
+------+---------------+
in_out
+------+-------+------+----+-----+
| Year | Month | Type | In | Out |
+------+-------+------+----+-----+
| 2020 | 1 | A | 20 | 20 |
| 2020 | 1 | A | 0 | 10 |
| 2020 | 2 | B | 20 | 0 |
| 2020 | 2 | B | 20 | 10 |
+------+-------+------+----+-----+
I'd like to get the result as follows:
Unfiltered:
+------+-------+------+---------+----+-----+------+
| Year | Month | Type | Balance | In | Out | Left |
+------+-------+------+---------+----+-----+------+
| 2020 | 1 | A | 0 | 20 | 20 | 0 |
| 2020 | 1 | B | 10 | 20 | 10 | 20 |
| 2020 | 2 | A | 0 | 20 | 10 | 10 |
| 2020 | 2 | B | 20 | 20 | 10 | 30 |
+------+-------+------+---------+----+-----+------+
Filtered (for example year/month 2020/2):
+------+-------+------+---------+----+-----+------+
| Year | Month | Type | Balance | In | Out | Left |
+------+-------+------+---------+----+-----+------+
| 2020 | 2 | A | 0 | 20 | 10 | 10 |
| 2020 | 2 | B | 20 | 20 | 10 | 30 |
+------+-------+------+---------+----+-----+------+
So while selecting a slicer for the year/month it should calculate balance before selected year/month and then show selected year/month values.
Edit: corrected start_balance table.
Is the sample data correct?
A -> the starting balance is 10, but in your unfiltered table example, it is 0.
Do you have any relationship between these tables?
Does opening balance always apply to the current year? What if 2021 appears in the in_out table? How do you know when the start balance started?
example without starting balance
If you want to show value breaking given filter you should use statement ALL or REMOVEFILTERS function (in Analysis Services 2019 and in Power BI since October 2019).
calculate(sum([in]) - sum([out]), all('in_out'[Year],'in_out'[Month]))
More helpful information:
https://www.sqlbi.com/articles/managing-all-functions-in-dax-all-allselected-allnoblankrow-allexcept/
I have data from every month in 2019 but only through September in 2020. Each row contains a MonthNo., corresponding to the calendar month, and a user ID entry. It looks like this
| Month | Year | ID | MonthNo. |
|-----------|------|--------|----------|
| January | 2019 | 611330 | 01 |
| January | 2019 | 174519 | 01 |
| January | 2019 | 380747 | 01 |
| February | 2019 | 882347 | 02 |
| February | 2019 | 633797 | 02 |
| February | 2019 | 863219 | 02 |
| March | 2019 | 189924 | 03 |
| March | 2019 | 241922 | 03 |
| March | 2019 | 563335 | 03 |
| April | 2019 | 648660 | 04 |
| April | 2019 | 363710 | 04 |
| April | 2019 | 606284 | 04 |
| May | 2019 | 296508 | 05 |
| May | 2019 | 287650 | 05 |
| May | 2019 | 599909 | 05 |
| June | 2019 | 513844 | 06 |
| June | 2019 | 891633 | 06 |
| June | 2019 | 138250 | 06 |
| July | 2019 | 126235 | 07 |
| July | 2019 | 853840 | 07 |
| July | 2019 | 713104 | 07 |
| August | 2019 | 180511 | 08 |
| August | 2019 | 451735 | 08 |
| August | 2019 | 818095 | 08 |
| September | 2019 | 512621 | 09 |
| September | 2019 | 674079 | 09 |
| September | 2019 | 914015 | 09 |
| October | 2019 | 132859 | 10 |
| October | 2019 | 560572 | 10 |
| October | 2019 | 272557 | 10 |
| November | 2019 | 984001 | 11 |
| November | 2019 | 815688 | 11 |
| November | 2019 | 902748 | 11 |
| December | 2019 | 880285 | 12 |
| December | 2019 | 167629 | 12 |
| December | 2019 | 772039 | 12 |
| January | 2020 | 116886 | 01 |
| January | 2020 | 386078 | 01 |
| February | 2020 | 291060 | 02 |
| February | 2020 | 970032 | 02 |
| March | 2020 | 907555 | 03 |
| March | 2020 | 560827 | 03 |
| April | 2020 | 938039 | 04 |
| April | 2020 | 721640 | 04 |
| May | 2020 | 131719 | 05 |
| May | 2020 | 415596 | 05 |
| June | 2020 | 589375 | 06 |
| June | 2020 | 623663 | 06 |
| July | 2020 | 577748 | 07 |
| July | 2020 | 999572 | 07 |
| August | 2020 | 630975 | 08 |
| August | 2020 | 442278 | 08 |
| September | 2020 | 993318 | 09 |
| September | 2020 | 413214 | 09 |
This example table has exactly 3 records for every month in 2019, and exactly 2 records for every month in 2020. So when I add a calculated field called MonthNotYearTraffic, defined by
// Averages ID count by month number only, intentionally ignoring year.
avgOver(count(ID), [{MonthNo.}])
I expect the following results
| MonthNo. | MonthNotYearTraffic |
|----------|---------------------|
| 01 | 2.5 |
| 02 | 2.5 |
| 03 | 2.5 |
| 04 | 2.5 |
| 05 | 2.5 |
| 06 | 2.5 |
| 07 | 2.5 |
| 08 | 2.5 |
| 09 | 2.5 |
| 10 | 3 |
| 11 | 3 |
| 12 | 3 |
since months 10-12 only have the three abovementioned 2019 entries. But instead, the results are:
I've tried this several different ways and combinations of the following (several of which I know to be insane, but others unsure):
at first not relying on custom, calculated fields
by partitioning on both month and year in the calculated field definition
by messing with level aware aggregations
by ensuring data types to agged by are strings/dimensions
No dice.
This seems like it should be straightforward technique, so any pointers would be nice. Thank you.
It looks as if you need to partition the count of your IDs by month and then divide that count by the count of years in which you have user IDs in that month.
Using your sample data I was able to get your desired output.
MonthNotYearTraffic = countover(ID,[Month],PRE_FILTER)/distinctCountOver(Year,[Month],PRE_FILTER)
I think the problem is that avgOver only works when you have the data displayed like you do in your first table where you are defining the values in the question. Since you are only showing the MonthNo. field and there are not many rows with that same MonthNo. value, there is only one row for each month in that partition so it's simply dividing the count by 1.
Maybe try something like count(ID) / count("MonthNo.")
I am using a measure not to display column subtotals. it works fine if I dont have any blanks.
Bond Count w/o =
VAR Bonds = CALCULATE(DISTINCTCOUNT(fact_Premium[PolicyNumber]))
RETURN
IF(
NOT(HASONEVALUE(dim_Date[Year])) && HASONEVALUE(dim_Date[Month]),
BLANK(),
Bonds
)
//HASONEVALUE returns TRUE when there is only one value in specified column
But in this case my matrix have blanks for particular months, and due to that measure doesnt work. So it brings subtotals for each column.
Is any way somehow to modify it so the logic would work for cases like that?
Thank you
.pbix can be found here: https://www.dropbox.com/s/h9xmpx6t997aqg9/TestBI.pbix?dl=0
Edit:
I have created a table with random data posted at the end of the post, where premium is the column with numbers.
The calculation below has two nested IF. The first forces a column subtotal using SELECTEDVALUE(Table[Year]). The second nested IF only consider the sum of premium when is equal to the grand total.
Sum_Premium = IF(SELECTEDVALUE('Table'[Year]),SUM('Table'[Premium]),if(sum('Table'[Premium])=CALCULATE(sum('Table'[Premium]),all('Table')),sum('Table'[Premium]),BLANK()))
Table
+------+-------+---------+
| Year | Month | Premium |
+------+-------+---------+
| 2017 | Jan | 10 |
| 2017 | Feb | 349 |
| 2017 | Mar | 406 |
| 2017 | Apr | 350 |
| 2017 | May | 31 |
| 2017 | Jun | 151 |
| 2017 | Jul | 266 |
| 2017 | Aug | 393 |
| 2017 | Sep | 278 |
| 2017 | Oct | 395 |
| 2017 | Nov | 119 |
| 2017 | Dec | 130 |
| 2018 | Jan | 190 |
| 2018 | Feb | 107 |
| 2018 | Mar | 248 |
| 2018 | Apr | 60 |
| 2018 | May | 302 |
| 2018 | Jun | 23 |
| 2018 | Jul | 248 |
| 2018 | Aug | 347 |
| 2018 | Sep | 31 |
| 2018 | Oct | 218 |
| 2018 | Nov | 326 |
| 2018 | Dec | 251 |
| 2019 | Jan | 173 |
| 2019 | Feb | 86 |
| 2019 | Mar | 29 |
| 2019 | Apr | 68 |
| 2019 | May | 19 |
| 2019 | Jun | 189 |
| 2019 | Jul | 261 |
| 2019 | Aug | 229 |
| 2019 | Sep | 338 |
| 2019 | Oct | 407 |
| 2019 | Nov | 409 |
| 2019 | Dec | 296 |
+------+-------+---------+
I would like to compare the same period of sessions per day. If i'm looking at Oct 10th 2018 to Oct. 16th 2018 (Wednesday to Tuesday), I would like to compare it to the same day range of last week:
+------+-------+-----+----------+-------------+--+
| year | month | day | sessions | last_period | |
+------+-------+-----+----------+-------------+--+
| 2018 | oct | 10 | 2000 | 2500 | |
| 2018 | oct | 11 | 2500 | 2400 | |
| 2018 | oct | 12 | 2600 | 2300 | |
| 2018 | oct | 13 | 2700 | 2450 | |
| 2018 | oct | 14 | 2400 | 2500 | |
| 2018 | oct | 15 | 2300 | 2200 | |
| 2018 | oct | 16 | 2000 | 1150 | |
+------+-------+-----+----------+-------------+--+
A simple formula can make it work based on the 7-day interval:
same_last_period = CALCULATE(SUM(table[Sessions]),DATEADD(table[Date],-7,DAY))
but I would like the formula to depend on a date slicer. Say if i wanted to look at the Oct 1-Oct 20. I would like my formula to change and look at the same period right before with the same amount of day intervals. Ultimately this would be graphed as well.
Try this:
same_last_period =
VAR DayCount = CALCULATE(DISTINCTCOUNT(table[Date]), ALLSELECTED(table[Date]))
RETURN CALCULATE(SUM(table[Sessions]), DATEADD(table[Date], -DayCount, DAY))
Edit:
This above doesn't work how I intended since you still have the year, month, and day in your filter context. That needs to be removed.
same_last_period =
VAR DayCount =
CALCULATE (
DISTINCTCOUNT ( 'table'[Date] ),
ALLSELECTED ( 'table'[Date] ),
ALLEXCEPT ( 'table', 'table'[Date] )
)
RETURN
CALCULATE (
SUM ( 'table'[Sessions] ),
DATEADD ( 'table'[Date], -DayCount, DAY ),
ALLEXCEPT ( 'table', 'table'[Date] )
)
The ALLEXCEPT removes any extra filter context except for Date.