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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

Producing SQL partition over in DAX

Hi All,

I have a requirement to add several columns that aggerate scores from the table while respecting slicers on the page.

neelofarshama_0-1627544985181.png

For adding these columns SQL is explained as below

SELECT  *,

          sum(scores) over(partition by newsroom, GROUP_ID, GROUP_VALUE) as sum_of_scores,

          sum(scores) over(partition by newsroom, GROUP_ID) as sum_of_scores_grouped_by_group_id,

          sum(scores) over(partition by newsroom, GROUP_VALUE) as sum_of_scores_grouped_by_group_value,

          sum(scores) over(partition by newsroom) as sum_of_scores_grouped_by_newsroom,

          count(distinct single_byline) over(partition by newsroom, GROUP_ID, GROUP_VALUE) as count_of_byline,

          count(distinct single_byline) over(partition by newsroom, GROUP_ID) as count_of_byline_grouped_by_group_id,

          count(distinct single_byline) over(partition by newsroom, GROUP_VALUE) as count_of_byline_grouped_by_group_value,

          count(distinct single_byline) over(partition by newsroom) as count_of_byline_grouped_by_newsroom

  FROM    Table

  WHERE   pubdate >= '2021-01-01' AND pubdate <= '2021-03-01'

Can we achieve this in Power BI please let me know.

Any help would be highly appreciated.

 

Thanks in Advance,

Neelofar Shama.

 

4 REPLIES 4
v-easonf-msft
Community Support
Community Support

Hi,  @Anonymous 

Could you please tell me whether your problem has been solved?

If yes, please accept the helpful answer as solution to close this thread. 
For now, there is no content of description in the thread. f you still need help, please share more details.

 

Best Regards,
Community Support Team _ Eason

amitchandak
Super User
Super User

@Anonymous , I power bi display * means you need put all column in visual. That too you need to have index/ ID column that will make sure data is grouped.

 

Allexcept can help in measure. The date can be filter from the slicer

 

calculate(sum(Table[scores]),allexcept(Table, [GROUP_ID], [GROUP_VALUE]))

 

or

 

calculate(sum(Table[scores]),filter(allselected(Table), [GROUP_ID] = max([GROUP_ID]) && [GROUP_VALUE] =max([GROUP_VALUE]) ))

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Thank you for this answer!  For over 12 hours I have been trying to figure out how to do a simple "COUNT OVER PARTITION" in DAX that honors slicer selections.  Your post helped me to finally put the pieces together.

My example project was a simple table of customers with state and "Local Type" (Rural or Urban). 

Table Name = CustomerLocal.

I put the data in a table and needed to count the number of customers per state (StateCnt) and have StateCnt adjust based on a slicer on LocalType. 

 

No Slicer:

CustomerIdLastNameStateLocalTypeStateCnt
1SMITHCARural2
2JONESCAUrban2
6FOSTERKSRural3
7KINGKSRural3
5LONGKSUrban3
3HAWKINSMORural2
4OWENMOUrban2


Slicer where LocalType = Rural:

CustomerIdLastNameStateLocalTypeStateCnt
1SMITHCARural1
6FOSTERKSRural2
7KINGKSRural2
3HAWKINSMORural1

 

Slicer where LocalType = Urban:

CustomerIdLastNameStateLocalTypeStateCnt
2JONESCAUrban1
5LONGKSUrban1
4OWENMOUrban1
 
 
With the help of this post I was finally able to figure out the correct DAX to cacluate StateCnt:

 

StateCnt =
     /* ALLSELECTED() removes ALL filters (namely row context) EXCEPT slicers,
      * [State] = SELECTEDVALUE([State]) filters to the State for current row to get 
      * the desired Partition for COUNT() */
    CALCULATE(
        COUNT(CustomerLocal[CustomerId]),
        FILTER(
            ALLSELECTED(CustomerLocal),
            CustomerLocal[State] = SELECTEDVALUE(CustomerLocal[State])
        )
    )


I know what it does and sorta know why it works, but would welcome any clear explanation someone can provide, particularly the interaction of ALLSELECTED within FILTER().

I probably need to provide a default value for SELECTEDVALUE(), but don't know under what data circumstances it would be required.

 

 
Thank you, amitchandak!
Anonymous
Not applicable

Thank you for the reply, I tried your solution but the results so not match with my SQL output in Power BI.

select

sum(scores) over(partition by newsroom, GROUP_ID) as sum_of_scores_grouped_by_group_id,
sum(scores) over(partition by newsroom, GROUP_VALUE) as sum_of_scores_grouped_by_group_value,

sum(scores) over(partition by newsroom) as sum_of_scores_grouped_by_newsroom,
count(distinct single_byline) over(partition by newsroom, GROUP_ID, GROUP_VALUE) as count _of_byline,
count(distinct single_byline) over(partition by newsroom, GROUP_ID) as count_of_byline_grouped_by_group_id,
count(distinct single_byline) over(partition by newsroom, GROUP_VALUE) as count_of_byline_grouped_by_group_value,
count(distinct single_byline) over(partition by newsroom) as count_of_byline_grouped_by_newsroom
FROM Table
WHERE pubdate >= '2021-01-01' AND pubdate <= '2021-03-01'

this is my SQL query which gives results as shown below

neelofarshama_0-1628271484583.png

In Power BI: I am getting 546491604 whereas in sql it is 6392482

neelofarshama_1-1628272036124.png

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.