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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Circuscazz
New Member

Conditional statements i.e countif

Hi 

 

I hope you can help?

I have a table like that shown below in Power BI. I'm trying to get a count of apples picked, appled delivered, bananas picked and bananas delivered by Date. 

 

Apples PickedApples deliveredBananas PickedBananas delivered
01/01/202402/01/202420/12/202324/12/2023
30/12/202331/12/202321/12/202324/12/2023
30/12/202302/01/202401/01/202401/01/2024
28/12/202302/01/202401/01/202402/01/2024

 

 

Therefore I would like to build the table below. Can this be done in power query? I would like it automatically based on today's date and descending for last 365 days? 

 

 Count of Apples PickedCount of Apples deliveredCount of Bananas PickedCount of Bananas Picked
02/01/20240301
01/01/20241021
31/12/20230100
30/12/20231000
29/12/20230000
28/12/20231000
27/12/20230000
26/12/20230000
25/12/20230002
24/12/20230020

 

 

5 REPLIES 5
jennratten
Super User
Super User

Hello - this is how you can do it...

jennratten_0-1704200652515.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJR0gEyjeBMEAvEMYZyTCCcWJ1oENcYWc7YEFmXITG6jJAtxWRC1RtZ4FcPF42NBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Apples Picked" = _t, #"Apples delivered" = _t, #"Bananas Picked" = _t, #"Bananas delivered" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Apples Picked", type text}, {"Apples delivered", type text}, {"Bananas Picked", type text}, {"Bananas delivered", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {}, "Attribute", "Value"),
    #"Duplicated Column" = Table.DuplicateColumn(#"Unpivoted Columns", "Value", "Date"),
    #"Pivoted Column" = Table.Pivot(#"Duplicated Column", List.Distinct(#"Duplicated Column"[Attribute]), "Attribute", "Value", List.Count),
    #"Changed Type1" = Table.TransformColumnTypes(#"Pivoted Column",{{"Date", type date}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type1", each Date.IsInPreviousNYears([Date], 1)),
    #"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Date", Order.Descending}})
in
    #"Sorted Rows"

Hi

 

Thanks, did you create a new data table, or can i amend to the original table? 

I used your data but changed the dates in the source step to M/d/yyyy to work with my locale.  To apply this solution to your data table, use everything except for the source step, like so (just change #"Changed Type" to the name of your last step):

    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {}, "Attribute", "Value"),
    #"Duplicated Column" = Table.DuplicateColumn(#"Unpivoted Columns", "Value", "Date"),
    #"Pivoted Column" = Table.Pivot(#"Duplicated Column", List.Distinct(#"Duplicated Column"[Attribute]), "Attribute", "Value", List.Count),
    #"Changed Type1" = Table.TransformColumnTypes(#"Pivoted Column",{{"Date", type date}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type1", each Date.IsInPreviousNYears([Date], 1)),
    #"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Date", Order.Descending}})
in
    #"Sorted Rows"

 

Thanks but i've stumbled on an issue, the table above are appended to a data table with lots of columns. However when i attempt to pivot the required column is doesnt work due to the additional columns and repeats the dates. Can I create a linked data table to focus solely on the pivioted columns? Let me know if i need to show an example

 

Hi @Circuscazz - Yes, you can create a linked reference, limit the columns omit those that are not needed and then perform the pivot.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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

Top Solution Authors
Top Kudoed Authors