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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
Leo_Tribaldo
Frequent Visitor

Custom Funtion para sumar el total de ventas proviene de otra tabla teniendo en cuenta dos variables

Hola,

Espero que vayais bien. Necesito vuestra ayuda. Ahora estoy empezando con el aprendizaje de funciones en M y me he quedado atascado con un supuesto.

Tabla Nº 1. tabla de ventas: 

 

let

#"tabla de ventas"=
#table (
type table [Producto= text, Fecha=date, Importe= number],
{
{"Producto A",#date(2023,03,01),1000},
{"Producto A",#date(2023,03,02),500},
{"Producto A",#date(2023,03,03),750},
{"Producto B",#date(2023,03,01),2000},
{"Producto B",#date(2023,03,02),1500},
{"Producto B",#date(2023,03,03),1750},
{"Producto C",#date(2023,03,01),500},
{"Producto C",#date(2023,03,02),250},
{"Producto C",#date(2023,03,03),375}

}
)
in
#"tabla de ventas"

Leo_Tribaldo_1-1682960959694.png

 

Tabla Nº 2. tabla de rangos

let

#"tabla de rangos"=
#table(

{"Producto","RangoFechas"},

{
{"Producto A",{"01/03/2023","03/03/2023"}},
{"Producto B",{"02/03/2023","03/03/2023"}},
{"Producto C",{"01/03/2023","02/03/2023"}}
}

)
in
#"tabla de rangos"

 

Leo_Tribaldo_2-1682960986931.png

 

 

 

Necesito una funcion para poder conseguir este resultado en la tabla Nº2

Leo_Tribaldo_0-1682960888566.png

 


Por favor necesito ayuda. 

Gracias

 

 

2 ACCEPTED SOLUTIONS
Syndicate_Admin
Administrator
Administrator

@Syndicate_Admin , ajusté la fecha para adaptarla al formato estadounidense en la Tabla 2. He añadido esta nueva columna

let 
_st = Date.From([DateRange]{0}),
_end = Date.From([DateRange]{1}),
_prd = [Product],
_data =  List.Sum(Table.SelectRows(Table1,each [Date] >= _st and [Date]<=_end and [Product] =_prd )[Amount])
in 
_data

También puede encontrar el archivo adjunto después de la firma

View solution in original post

Syndicate_Admin
Administrator
Administrator

let
    #"sales table"=
        #table (
            type table [Product= text, Date=date, Amount= number],
            {
                {"Product A",#date(2023,03,01),1000},
                {"Product A",#date(2023,03,02),500},
                {"Product A",#date(2023,03,03),750},
                {"Product B",#date(2023,03,01),2000},
                {"Product B",#date(2023,03,02),1500},
                {"Product B",#date(2023,03,03),1750},
                {"Product C",#date(2023,03,01),500},
                {"Product C",#date(2023,03,02),250},
                {"Product C",#date(2023,03,03),375}
            }
        ),
    #"range table"=
    #table(
        {"Product","DateRange"},
        {
            {"Product A",{"01/03/2023","03/03/2023"}},
            {"Product B",{"02/03/2023","03/03/2023"}},
            {"Product C",{"01/03/2023","02/03/2023"}}
        }
    ),
    Joined = Table.NestedJoin(#"range table", "Product", #"sales table", "Product", "Joined", JoinKind.LeftOuter),
    #"Filtered Amount" = Table.ReplaceValue(Joined, each List.Transform([DateRange], Date.From), null, (x,y,z) => List.Sum(Table.SelectRows(x, each y{0}<=[Date] and [Date]<=y{1})[Amount]), {"Joined"})
in
    #"Filtered Amount"

ThxAlot_0-1682968265514.png

View solution in original post

3 REPLIES 3
Leo_Tribaldo
Frequent Visitor

Hola,

Excelente solucion. Muchas gracias por tu ayuda. Seguimos aprendiendo de este maravilloso lenguaje M.

Seguimos en contacto.

Syndicate_Admin
Administrator
Administrator

let
    #"sales table"=
        #table (
            type table [Product= text, Date=date, Amount= number],
            {
                {"Product A",#date(2023,03,01),1000},
                {"Product A",#date(2023,03,02),500},
                {"Product A",#date(2023,03,03),750},
                {"Product B",#date(2023,03,01),2000},
                {"Product B",#date(2023,03,02),1500},
                {"Product B",#date(2023,03,03),1750},
                {"Product C",#date(2023,03,01),500},
                {"Product C",#date(2023,03,02),250},
                {"Product C",#date(2023,03,03),375}
            }
        ),
    #"range table"=
    #table(
        {"Product","DateRange"},
        {
            {"Product A",{"01/03/2023","03/03/2023"}},
            {"Product B",{"02/03/2023","03/03/2023"}},
            {"Product C",{"01/03/2023","02/03/2023"}}
        }
    ),
    Joined = Table.NestedJoin(#"range table", "Product", #"sales table", "Product", "Joined", JoinKind.LeftOuter),
    #"Filtered Amount" = Table.ReplaceValue(Joined, each List.Transform([DateRange], Date.From), null, (x,y,z) => List.Sum(Table.SelectRows(x, each y{0}<=[Date] and [Date]<=y{1})[Amount]), {"Joined"})
in
    #"Filtered Amount"

ThxAlot_0-1682968265514.png

Syndicate_Admin
Administrator
Administrator

@Syndicate_Admin , ajusté la fecha para adaptarla al formato estadounidense en la Tabla 2. He añadido esta nueva columna

let 
_st = Date.From([DateRange]{0}),
_end = Date.From([DateRange]{1}),
_prd = [Product],
_data =  List.Sum(Table.SelectRows(Table1,each [Date] >= _st and [Date]<=_end and [Product] =_prd )[Amount])
in 
_data

También puede encontrar el archivo adjunto después de la firma

Helpful resources

Announcements
March PBI video - carousel

Power BI Monthly Update - March 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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

Top Kudoed Authors