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
Syndicate_Admin
Administrator
Administrator

DAX Function Optimization

Good evening forum,

Below I share the code of a DAX function that I would like to optimize. The idea is that you can get the genre that has the most streams. I read that one possibility might be to implement a virtual table in addition to using the MAXX function, but I'm not sure about that. What suggestions do you have? Thank you very much!

Code

Most listened to genre =
WERE ALTERNATIVA_INDEPENDIENTE = [ALTERNATIVE/INDEPENDENT Streams]
WERE Saves= [Stream Saves]
WERE BLUES = [Streams BLUES]
WERE COUNTRY = [Streams COUNTRY]
WERE ELECTRONICA = [Streams ELECTRONICA]
WERE HIP_HOP_RAP = [Streams HIP-HOP/RAP]
WERE JAZZ = [Streams JAZZ]
WERE POP = [Streams POP]
WERE RB_SOUL = [Streams R&B/SOUL]
WERE REGGAE = [Streams REGGAE]
WERE REGIONAL = [Streams REGIONAL]
WERE REGGAETON = [REGGAETON Streams]
WERE ROCK = [ROCK Streams]
WERE SAMBA = [Streams SAMBA]
WERE URBAN = [URBAN Streams]
WERE NO_DETERMINADO = [Streams IN DETERMINED]
RETURN
SWITCH(
TRUE(),
ALTERNATIVA_INDEPENDIENTE > BACHATA & ALTERNATIVA_INDEPENDIENTE > BLUES & ALTERNATIVA_INDEPENDIENTE > COUNTRY &
ALTERNATIVA_INDEPENDIENTE > ELECTRONICA & ALTERNATIVA_INDEPENDIENTE > HIP_HOP_RAP & ALTERNATIVA_INDEPENDIENTE > JAZZ &
ALTERNATIVA_INDEPENDIENTE > POP & ALTERNATIVA_INDEPENDIENTE > RB_SOUL & ALTERNATIVA_INDEPENDIENTE > REGGAE &
ALTERNATIVA_INDEPENDIENTE > REGIONAL & ALTERNATIVA_INDEPENDIENTE > REGUETON & ALTERNATIVA_INDEPENDIENTE > ROCK &
ALTERNATIVA_INDEPENDIENTE > SAMBA & ALTERNATIVA_INDEPENDIENTE > URBANO &
ALTERNATIVA_INDEPENDIENTE > NO_DETERMINADO, "ALTERNATIVE/INDEPENDENT",
BACHATA > ALTERNATIVA_INDEPENDIENTE & BACHATA > BLUES & BACHATA > COUNTRY & BACHATA > ELECTRONICA &
BACHATA > HIP_HOP_RAP & BACHATA > JAZZ & BACHATA > POP & BACHATA > RB_SOUL & BACHATA > REGGAE &
BACHATA > REGIONAL & BACHATA > REGUETON & BACHATA > ROCK & BACHATA > SAMBA & BACHATA > URBANO
& BACHATA > NO_DETERMINADO, "BACHATA",
BLUES > ALTERNATIVA_INDEPENDIENTE & BLUES > BACHATA & BLUES > COUNTRY & BLUES > ELECTRONICA &
BLUES > HIP_HOP_RAP & BLUES > JAZZ & BLUES > POP & BLUES > RB_SOUL & BLUES > REGGAE &
REGIONAL > BLUES & BLUES > REGUETON & BLUES > ROCK & BLUES > SAMBA & BLUES > URBANO
& BLUES > NO_DETERMINADO, "BLUES",
COUNTRY > ALTERNATIVA_INDEPENDIENTE && COUNTRY > BACHATA && COUNTRY > BLUES && COUNTRY > ELECTRONICA &&
COUNTRY > HIP_HOP_RAP && COUNTRY > JAZZ && COUNTRY > POP && COUNTRY > RB_SOUL && COUNTRY > REGGAE &&
COUNTRY > REGIONAL && COUNTRY > REGUETON && COUNTRY > ROCK && COUNTRY > SAMBA && COUNTRY > URBANO
&& COUNTRY > NO_DETERMINADO, "COUNTRY",
ELECTRONICA > ALTERNATIVA_INDEPENDIENTE && ELECTRONICA > BACHATA && ELECTRONICA > BLUES && ELECTRONICA > COUNTRY &&
ELECTRONICA > HIP_HOP_RAP && ELECTRONICA > JAZZ && ELECTRONICA > POP && ELECTRONICA > RB_SOUL && ELECTRONICA > REGGAE &&
ELECTRONICA > REGIONAL && ELECTRONICA > REGUETON && ELECTRONICA > ROCK && ELECTRONICA > SAMBA && ELECTRONICA > URBANO
&& ELECTRONICA > NO_DETERMINADO, "ELECTRONICA",
HIP_HOP_RAP > ALTERNATIVA_INDEPENDIENTE & HIP_HOP_RAP > BACHATA & HIP_HOP_RAP > BLUES & HIP_HOP_RAP > COUNTRY &
HIP_HOP_RAP > ELECTRONICA & HIP_HOP_RAP > JAZZ & HIP_HOP_RAP > POP & HIP_HOP_RAP > RB_SOUL & HIP_HOP_RAP > REGGAE &
HIP_HOP_RAP > REGIONAL & HIP_HOP_RAP > REGUETON & HIP_HOP_RAP > ROCK & HIP_HOP_RAP > SAMBA & HIP_HOP_RAP > URBANO
& HIP_HOP_RAP > NO_DETERMINADO, "HIP-HOP/RAP",
JAZZ > ALTERNATIVA_INDEPENDIENTE & JAZZ > BACHATA & JAZZ > BLUES & JAZZ > COUNTRY &&
JAZZ > ELECTRONICA & JAZZ > HIP_HOP_RAP & JAZZ > POP & JAZZ > RB_SOUL & JAZZ > REGGAE &&
REGIONAL > JAZZ & JAZZ > REGUETON & JAZZ > ROCK & JAZZ > SAMBA & JAZZ > URBANO
& JAZZ > NO_DETERMINADO, "JAZZ",
POP > ALTERNATIVA_INDEPENDIENTE & POP > BACHATA & POP > BLUES & POP > COUNTRY &&
POP > ELECTRONICA & POP > HIP_HOP_RAP & POP > JAZZ & POP > RB_SOUL & POP > REGGAE &
POP > REGIONAL & POP > REGUETON & POP > ROCK & POP > SAMBA & POP > URBANO
& POP > NO_DETERMINADO, "POP",
RB_SOUL > ALTERNATIVA_INDEPENDIENTE & RB_SOUL > BACHATA & RB_SOUL > BLUES & RB_SOUL > COUNTRY &
RB_SOUL > ELECTRONICA & RB_SOUL > HIP_HOP_RAP & RB_SOUL > JAZZ & RB_SOUL > POP & RB_SOUL > REGGAE &
RB_SOUL > REGIONAL & RB_SOUL > REGUETON & RB_SOUL > ROCK & RB_SOUL > SAMBA & RB_SOUL > URBANO
& RB_SOUL > NO_DETERMINADO, "R&B/SOUL",
REGGAE > ALTERNATIVA_INDEPENDIENTE & REGGAE > BACHATA & REGGAE > BLUES & REGGAE > COUNTRY &
REGGAE > ELECTRONICA & REGGAE > HIP_HOP_RAP & REGGAE > JAZZ & REGGAE > POP & REGGAE > RB_SOUL &
REGGAE > REGIONAL & REGGAE > REGUETON & REGGAE > ROCK & REGGAE > SAMBA &&REGGAE > URBANO
& REGGAE > NO_DETERMINADO, "REGGAE",
REGIONAL > ALTERNATIVA_INDEPENDIENTE && REGIONAL > BACHATA && REGIONAL > BLUES && REGIONAL > COUNTRY &&
REGIONAL > ELECTRONICA && REGIONAL > HIP_HOP_RAP && REGIONAL > JAZZ && REGIONAL > POP && REGIONAL > RB_SOUL &&
REGIONAL > REGGAE & REGIONAL > REGUETON & REGIONAL > ROCK & REGIONAL > SAMBA & REGIONAL > URBANO
&& REGIONAL > NO_DETERMINADO, "REGIONAL",
REGUETON > ALTERNATIVA_INDEPENDIENTE & REGUETON > BACHATA & REGUETON > BLUES & REGUETON > COUNTRY &
REGGAETON > ELECTRONICA & & REGGAETON > HIP_HOP_RAP & REGGAETON > JAZZ & & REGGAETON > POP & & REGGAETON > RB_SOUL &&
REGUETON > REGGAE & REGUETON > REGIONAL & REGUETON > ROCK & REGUETON > SAMBA & REGUETON > URBANO
& REGUETON > NO_DETERMINADO, "Reggaeton",
ROCK > ALTERNATIVA_INDEPENDIENTE & ROCK > BACHATA & ROCK > BLUES & ROCK > COUNTRY &
ROCK > ELECTRONICA & ROCK > HIP_HOP_RAP & ROCK > JAZZ & ROCK > POP & ROCK > RB_SOUL &
ROCK > REGGAE & ROCK > REGIONAL & ROCK > REGUETON & ROCK > SAMBA & ROCK > URBANO
& ROCK > NO_DETERMINADO, "ROCK",
SAMBA > ALTERNATIVA_INDEPENDIENTE & SAMBA > BACHATA & SAMBA > BLUES & SAMBA > COUNTRY & SAMBA
SAMBA > ELECTRONICA & SAMBA > HIP_HOP_RAP & SAMBA > JAZZ & SAMBA > POP & SAMBA > RB_SOUL &
SAMBA > REGGAE && SAMBA > REGIONAL && SAMBA > REGUETON && SAMBA > ROCK && SAMBA > URBANO
& SAMBA > NO_DETERMINADO, "SAMBA",
URBAN > ALTERNATIVA_INDEPENDIENTE& URBAN & URBAN > BACHATA & URBAN > BLUES & URBAN > COUNTRY & &
URBANO > ELECTRONICA & URBANO > HIP_HOP_RAP & URBANO > JAZZ & URBANO > POP & URBANO > RB_SOUL &
URBAN > REGGAE & URBAN > REGIONAL & URBAN > REGUETON & URBANO > ROCK & URBAN > SAMBA
& URBANO > NO_DETERMINADO, "URBAN",
"IN THE DETERMINED"
)

2 REPLIES 2
MFelix
Super User
Super User

Hi @Jeremias_Mena08 ,

 

Not sure of what type of function you are having here what do they calculate however  I assume that each of this stiles are saved on a column somewhere correct?

I would add something similar to this:

Most Stream =
_TempTable = ADDCOLUMNS(VALUES(Table[Music Category]), "Streams",[Streams])
Return

MAXX ( TOPN(1, temp_table, [Sales],DESC), Table[Music Category])

 

The [Streams] syntax should be a measure on your model that counts and/or sum the number of streams.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



In this case, the number of streams that a particular genre has is calculated through a measure

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.