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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Agregación y cálculo dinámicos

Hola chicos

Estoy tratando de traducir una consulta sql anidada en power bi y mantener la capacidad de usar los campos de primer nivel como segmentaciones de datos

Aquí está el escenario

Los datos sin procesar se ven así

identificación del clienteorder_numberPaísCantidadFecha
A 1Vie10001/01/2020
A 1Vie10001/02/2020
A 1Vie10001/03/2020
A 2Uk10001/01/2020
A 2Uk10001/02/2020
B3Nos10001/02/2020
B3Nos10001/03/2020
B4Vie10001/02/2020
B4Vie10001/03/2020
C5Nos10001/02/2020
C5Nos10001/03/2020

Para mi cálculo necesito hacer un primer grupo por y pasar de order_number a customer_id nivel y debe dar este

identificación del clienteFechaprimera sumaRowNumberis_new_customer
A01/01/202020011
A01/02/202020020
A01/03/202010030
B01/02/202020011
B01/03/202020020
C01/02/202010011
C01/03/202010020

También he añadido un número de fila para ayudarme a contar el nuevo cliente (donde row_number 1 )

Y mi necesidad final debería ser así.

Fechacontar nuevo clientecliente totalsegunda suma
01/01/202011200
01/02/202023500
01/03/202003400

El reto aquí es crear estas medidas y ser capaz de filtrar por el país

Si selecciono "US" como país, debería tener este resultado

Fechacontar nuevo clientecliente totalsegunda suma
01/01/2020000
01/02/202022200
01/03/202002200

Lo intenté con una tabla de cálculo, pero es estática y no puedo usar los filtros del primer nivel

He intentado crear una medida dinámica donde he construido la última tabla de agregación para contar a los nuevos clientes, pero no se ensucia en el row_number 1, además de que es un código dax horrible, soy nuevo en power bi y ni siquiera sé si estoy en el camino correcto

aquí está el código

count new customer = 
-- var v = filter(
    VAR a =
        ADDCOLUMNS (
            SUMMARIZE (
                ADDCOLUMNS (
                    GROUPBY (
                        'Fact  flat';
                        'Fact  flat'[account_id];
                        'Fact  flat'[po_number];
                        'Fact  flat'[the_month];
                        'Fact  flat'[continuity];
                        'Fact  flat'[mrr_transposed]
                    );
                    "continuity_new_customers"; IF ( 'Fact  flat'[continuity] = 0; 1; 'Fact  flat'[continuity] )
                );
                [account_id];
                [the_month];
                [continuity_new_customers];
                "sum mrr trans"; SUM ( 'Fact  flat'[mrr_transposed] )
            );
            "RowNumberEmbed"; RANKX (
                FILTER (
                    GROUPBY ( 'Fact  flat'; 'Fact  flat'[account_id]; 'Fact  flat'[the_month] );
                    'Fact  flat'[account_id] = EARLIER ( 'Fact  flat'[account_id] )
                );
                [the_month];
                ;
                ASC
            )
        )

    var b = 
        ADDCOLUMNS( 
            a;
            "Previous_Value"; CALCULATE (
        
                MAxX(      SUMMARIZE (
                                            GROUPBY (
                                                'Fact  flat';
                                                'Fact  flat'[account_id];
                                                'Fact  flat'[po_number];
                                                'Fact  flat'[the_month];
                                                'Fact  flat'[mrr_transposed]
                                            );      
                                        [account_id];
                                        [the_month];
                                        "sum mrr trans"; SUM ( 'Fact  flat'[mrr_transposed] )
                                    )          
                                    ; [sum mrr trans] )

    
                    ; FILTER ( a; [account_id]= EARLIER([account_id])  && [the_month] <   EARLIER([the_month]) && [RowNumberEmbed] = EARLIER([RowNumberEmbed]  ) -1
            ))

        )


var c = 
    filter(
             ADDCOLUMNS(b;
                    "row_number_new_customer"; if([Previous_Value] <>0 ; [RowNumberEmbed];1))
        ; [RowNumberEmbed] = 1
        ) 
return  COUNTX(c;[account_id])

¿Puedes por favor ayudarme en este por favor, esta es la tercera semana donde estoy atrapado en esta tarea

Gracias de antemano por su ayuda

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

Aquí está la segunda suma. PBIX con las tres medidas se incluye como un archivo adjunto a continuación.

second sum = 
    VAR __Date = MAX('Table'[date])
    VAR __Country = SELECTCOLUMNS('Table',"Country",[country])
    VAR __Table = 
        SUMMARIZE(
            FILTER(
                ALL('Table'),
                [country] IN __Country
            ),
            [date],
            "Sum",SUM('Table'[amount])
        )
    VAR __Sum = SUMX(FILTER(__Table,[date] = __Date),[Sum])
RETURN
    IF(ISBLANK(__Sum),0,__Sum)


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

Aquí está la segunda suma. PBIX con las tres medidas se incluye como un archivo adjunto a continuación.

second sum = 
    VAR __Date = MAX('Table'[date])
    VAR __Country = SELECTCOLUMNS('Table',"Country",[country])
    VAR __Table = 
        SUMMARIZE(
            FILTER(
                ALL('Table'),
                [country] IN __Country
            ),
            [date],
            "Sum",SUM('Table'[amount])
        )
    VAR __Sum = SUMX(FILTER(__Table,[date] = __Date),[Sum])
RETURN
    IF(ISBLANK(__Sum),0,__Sum)


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Thx mucho @Greg_Deckler !!! 😄

¡Feliz de ayudar! 🙂

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Greg_Deckler
Super User
Super User

Cliente total:

total customer = 
    VAR __Date = MAX('Table'[date])
    VAR __Country = SELECTCOLUMNS('Table',"Country",[country])
    VAR __Table = 
        SUMMARIZE(
            FILTER(
                ALL('Table'),
                [country] IN __Country
            ),
            [customer id],
            "Date",MIN('Table'[date])
        )
    VAR __Count = COUNTROWS(FILTER(__Table,[Date]<=__Date))
RETURN
    IF(ISBLANK(__Count),0,__Count)

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Greg_Deckler
Super User
Super User

Aquí está contar nuevo cliente:

count new customer = 
    VAR __Date = MAX('Table'[date])
    VAR __Country = SELECTCOLUMNS('Table',"Country",[country])
    VAR __Table = 
        SUMMARIZE(
            FILTER(
                ALL('Table'),
                [country] IN __Country
            )
            ,[customer id],"Date",MIN('Table'[date])
        )
    VAR __Count = COUNTROWS(FILTER(__Table,[Date] = __Date))
RETURN
    IF(ISBLANK(__Count),0,__Count)


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.