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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Syndicate_Admin
Administrator
Administrator

Custom Funtion to add the total sales comes from another table taking into account two variables

Hello

I hope you do well. I need your help. Now I'm starting with learning functions in M and I've been stuck with an assumption.

Table Nº 1. Sales table:

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}

}
)
in
#"sales table"

Leo_Tribaldo_1-1682960959694.png

Table No. 2. Range table

Let

#"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"}}
}

)
in
#"range table"

Leo_Tribaldo_2-1682960986931.png

I need a function to be able to achieve this result in table Nº2

Leo_Tribaldo_0-1682960888566.png


Please I need help.

Thank you

3 REPLIES 3
Syndicate_Admin
Administrator
Administrator

Hello

Excellent solution. Thank you very much for your help. We continue to learn from this wonderful M language.

We keep in touch.

ThxAlot
Super User
Super User

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



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LeanAndPractise(Everyday)


)



amitchandak
Super User
Super User

@Syndicate_Admin , I adjusted the date to suit the US format in Table 2. I added this new column

 

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

 

You can also find the file attached after signature

 

 

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

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

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! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.