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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Sumx avec des dates dynamiques

Bonjour à tous,

 

Je cherche à faire une mesure simple mais je ne trouve pas la solution.

 

Mon modèle est composé d'une table avec 4 colonnes : ID;Date_Début;Date_Fin;Prix_journalier et d'une table DATE.

 

L'objectif est d'obtenir année par année la somme des Prix_Journalier pour la période écoulé.

 

Par exemple, le produit 1 à une Date_Début = 18/12/2020 et Date_FIN = 05/01/2021 et un prix journalier = 1, j'aimerais retourner un tableau avec pour valeur en 2020 : 13 et pour 2021 : 5

 

J'ai crée une mesure = 

SUMX(
   'Produit';
    DATEDIFF('Produit'[Date_Début];min(MAX('DATE'[Date]);'Produit'[DATE_FIN);DAY))
 
Mais lors que je crée le visuel je retrouve le bon montant pour 2020 mais un montant vide pour 2021
 
J'ai crée une seconde mesure permettant le total cumulé qui me permet d'avoir un visuel avec l'année 2020 et 2021 mais je n'arrive pas par la suite à avoir le montant pour 2021 uniquement.
 
Pouvez-vous m'aider ?
1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Bonjour @Anonymous 

 

First of all, excuse me, as I am replying in English after reading an English translation of your post (via Google Translate).

 

From what you've described, I understand that you want to sum the values of Prix_journalier, repeated for each date that is both within the currently filtered date range from the DATE table and within the range [ Date_Début, Date_FIN]

 

I am assuming that there is no relationship between the Produit and 'DATE' tables (but the code can be modified to work if there is a relationship).

 

Your idea of using SUMX over the Produit table is sensible. I would just modify it to something like below. This defines a variable DateCount which is the number of dates in the intersection of the filtered date from 'DATE' and the range defined by the current row. DateCount is then multiplied by the price for the current row:

Test Measure = 
SUMX ( 
    Produit,
    VAR CurrentPrice =
        Produit[Prix_journalier]
    VAR CurrentDateRange =
        DATESBETWEEN ( 'DATE'[Date], Produit[Date_Début], Produit[Date_FIN] )
    VAR DateCount =
        CALCULATE (
            COUNTROWS ( 'DATE' ),
            KEEPFILTERS ( CurrentDateRange )
        )
    RETURN
        CurrentPrice * DateCount
)

 

You may want to check this for "off by one" errors, as I'm not certain whether the first/last dates should always be included.

 

Does this or something similar work for you?

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

1 REPLY 1
OwenAuger
Super User
Super User

Bonjour @Anonymous 

 

First of all, excuse me, as I am replying in English after reading an English translation of your post (via Google Translate).

 

From what you've described, I understand that you want to sum the values of Prix_journalier, repeated for each date that is both within the currently filtered date range from the DATE table and within the range [ Date_Début, Date_FIN]

 

I am assuming that there is no relationship between the Produit and 'DATE' tables (but the code can be modified to work if there is a relationship).

 

Your idea of using SUMX over the Produit table is sensible. I would just modify it to something like below. This defines a variable DateCount which is the number of dates in the intersection of the filtered date from 'DATE' and the range defined by the current row. DateCount is then multiplied by the price for the current row:

Test Measure = 
SUMX ( 
    Produit,
    VAR CurrentPrice =
        Produit[Prix_journalier]
    VAR CurrentDateRange =
        DATESBETWEEN ( 'DATE'[Date], Produit[Date_Début], Produit[Date_FIN] )
    VAR DateCount =
        CALCULATE (
            COUNTROWS ( 'DATE' ),
            KEEPFILTERS ( CurrentDateRange )
        )
    RETURN
        CurrentPrice * DateCount
)

 

You may want to check this for "off by one" errors, as I'm not certain whether the first/last dates should always be included.

 

Does this or something similar work for you?

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.