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
SharmilaBrisca
Frequent Visitor

To Find Next Based on Financial Year (BASED ON One DATE COLUMN AND ACCOUNT CODE IN POWER QUERY)

 

My data contains 4 column Date,Account Code,Company and Amount.Now I want to create new column in power query editor and the new column should return the next date based on Date, Account Code and Company.....
Financial Year-Example (Starts from 01-04-2023 to End 01-03-2024)

SharmilaBrisca_2-1706174846675.png

For Example: In First Row for Account Code-89888 and Company 234 Date is 01-03-2023, now the new column(Next Date) which I create should return the same date(01-03-2023) for the same Account Code and Company 
In Second Row for Account Code-98789 and Company 876 Date is 01-06-2023, now the new column(Next Date) which I create should return the following date in the same Year(01-09-2023) for the same Account Code and Company 
If there is no Next following date for the Particular Account Code and Company then it should return year end date(01-03-20..) for the particular date

SharmilaBrisca_3-1706176763564.png

All these needs to be done in Power Query Editor

1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @SharmilaBrisca, you posted same question again. I replied to you with solution yesterday here,

but let me paste it again:

dufoq3_0-1706350277722.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dc9LDoAgDEXRvTDWpB+gr2sx7n8blhhMozhpJye9cByFeCfdhUTLVuAAYovWmA6icm636dM4DD6s9ZjWk/FkusVutY1rLRlbt2A/d1JLU4t53eLcev7FLE4yYsTj0XF0mvpj4K87/DWiLyOLVgtzXg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Account Code" = _t, Company = _t, Amount = _t]),
    YourSource = Source,
    AddedIndex = Table.AddIndexColumn(YourSource, "Index", 0, 1, Int64.Type),
    Ad_FiscalYear = Table.AddColumn(AddedIndex, "Fiscal Year", each if Date.Month(Date.From([Date])) < 4 then Date.Year(Date.From([Date])) -1 else Date.Year(Date.From([Date])), Int64.Type),
    GroupedRows = Table.Group(Ad_FiscalYear, {"Account Code", "Company", "Fiscal Year"}, {{"All1", each _, type table}, {"All2", each _, type table}}),
    ExpandedAll1 = Table.ExpandTableColumn(GroupedRows, "All1", {"Date", "Amount", "Index"}, {"Date", "Amount", "Index"}),
    SortedRows = Table.Sort(ExpandedAll1,{{"Index", Order.Ascending}}),
    Ad_NextDate = Table.AddColumn(SortedRows, "Next Date", each 
        Date.From(
            Record.FieldOrDefault(
                Table.SelectRows(
                    [All2],
                    (r)=> r[Index] > [Index]
                ){0}?,
                "Date",
                #date([Fiscal Year] +1, 3, 1)
            )
        ), type date),
    RemovedOtherColumns = Table.SelectColumns(Ad_NextDate,{"Date", "Account Code", "Company", "Amount", "Next Date"}),
    ChangedType = Table.TransformColumnTypes(RemovedOtherColumns,{{"Date", type date}, {"Account Code", Int64.Type}, {"Company", Int64.Type}, {"Amount", Currency.Type}})
in
    ChangedType

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

4 REPLIES 4
dufoq3
Super User
Super User

Hi @SharmilaBrisca, you posted same question again. I replied to you with solution yesterday here,

but let me paste it again:

dufoq3_0-1706350277722.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dc9LDoAgDEXRvTDWpB+gr2sx7n8blhhMozhpJye9cByFeCfdhUTLVuAAYovWmA6icm636dM4DD6s9ZjWk/FkusVutY1rLRlbt2A/d1JLU4t53eLcev7FLE4yYsTj0XF0mvpj4K87/DWiLyOLVgtzXg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Account Code" = _t, Company = _t, Amount = _t]),
    YourSource = Source,
    AddedIndex = Table.AddIndexColumn(YourSource, "Index", 0, 1, Int64.Type),
    Ad_FiscalYear = Table.AddColumn(AddedIndex, "Fiscal Year", each if Date.Month(Date.From([Date])) < 4 then Date.Year(Date.From([Date])) -1 else Date.Year(Date.From([Date])), Int64.Type),
    GroupedRows = Table.Group(Ad_FiscalYear, {"Account Code", "Company", "Fiscal Year"}, {{"All1", each _, type table}, {"All2", each _, type table}}),
    ExpandedAll1 = Table.ExpandTableColumn(GroupedRows, "All1", {"Date", "Amount", "Index"}, {"Date", "Amount", "Index"}),
    SortedRows = Table.Sort(ExpandedAll1,{{"Index", Order.Ascending}}),
    Ad_NextDate = Table.AddColumn(SortedRows, "Next Date", each 
        Date.From(
            Record.FieldOrDefault(
                Table.SelectRows(
                    [All2],
                    (r)=> r[Index] > [Index]
                ){0}?,
                "Date",
                #date([Fiscal Year] +1, 3, 1)
            )
        ), type date),
    RemovedOtherColumns = Table.SelectColumns(Ad_NextDate,{"Date", "Account Code", "Company", "Amount", "Next Date"}),
    ChangedType = Table.TransformColumnTypes(RemovedOtherColumns,{{"Date", type date}, {"Account Code", Int64.Type}, {"Company", Int64.Type}, {"Amount", Currency.Type}})
in
    ChangedType

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

rubayatyasmin
Super User
Super User

Hi, @SharmilaBrisca 

 

try this one

= Table.AddColumn(IndexedTable, "Next Date", each let
CurrentDate = [Date],
currentCode = [Account Code],
currentComp = [Company],
currentIndex = [Index],
nextRow = IndexedTable{currentIndex + 1}?
in
if Date.Month([Date]) = 3 then [Date] else if nextRow <> null and nextRow[Account Code] = currentCode and nextRow[Company] = currentComp then
nextRow[Date]
else
#date(2024, 3, 1))


this should work. 

 

rubayatyasmin_1-1706191379402.png

 

 


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


Hi @rubayatyasmin
I am getting below image by using your solution

SharmilaBrisca_4-1706277661422.png

but in my scenario, 
For row 11 I should get 01-03-2021( because fiscal year Starts at April and ends at March)
Example(Year Starts from 01-04-2020 and End in 01-03-2021)
For row 12 I should get 01-03-2022

if we don't have following date in particular financial year then it should return the financial year end date(Date(01)-Month(03)-Year(Financial Year)


 

Hey the solution is pretty correct. You go to the condition I wrote for the next year column. There I added 1-03-24 as else so you are getting year 24 for this.  So you need to adjust the condition a little bit. And you should be good to go


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


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.

Top Solution Authors
Top Kudoed Authors