SOLVED

Using a Date Parameter in a Calculation substraction latest year from 2010

%3CLINGO-SUB%20id%3D%22lingo-sub-1809951%22%20slang%3D%22en-US%22%3EUsing%20a%20Date%20Parameter%20in%20a%20Calculation%20substraction%20latest%20year%20from%202010%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1809951%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20parameter%20that%20is%20a%20year.%26nbsp%3B%20Can%20it%20be%20passed%20to%20a%20calculated%20field%3F%26nbsp%3B%20The%20parameter%20will%20always%20grab%20the%20latest%20year%20and%20be%20subtracted%20from%202010.%26nbsp%3B%20So%2C%20if%20I%20get%20new%20data%20for%202020%20the%20Parameter%20will%20grab%20the%20latest%20year%20and%20the%20calculation%20will%20use%20it%20for%20the%20subtraction%20(2019-2010).%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1809951%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1810107%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20a%20Date%20Parameter%20in%20a%20Calculation%20substraction%20latest%20year%20from%202010%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1810107%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F434928%22%20target%3D%22_blank%22%3E%40lmk001%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAbsolutely%20it's%20possible.%20I%20don't%20see%20in%20your%20spreadsheet%20any%20spot%20where%20you're%20trying%20to%20do%20it%2C%20however.%20So%20it's%20hard%20to%20give%20anything%20specific.%20Let%20me%20just%20offer%20the%20following%20and%20invite%20you%2C%20if%20needed%2C%20to%20post%20a%20revised%20spreadsheet%20where%20you%20might%20include%20the%20formula%20where%20you're%20trying%20to%20make%20the%20calculation%20and%20not%20having%20it%20work.%3C%2FP%3E%3CP%3EFirst%2C%20though%2C%20let's%20see%20if%20a%20few%20pointers%20can%20help%20you%20make%20it%20work.%3C%2FP%3E%3COL%3E%3CLI%3EMake%20sure%20that%20those%20numbers%20representing%20the%20years%20(the%20headings%20in%20some%20of%20your%20columns)%20are%20numbers%20and%20not%20text.%26nbsp%3B%20That%20appears%20to%20be%20the%20case.%3C%2FLI%3E%3CLI%3EThen%20if%20you're%20using%20a%20full%20date%20(in%20Excel's%20%22short%20date%22%20format%20or%20some%20other)%2C%20just%20enclose%20that%20in%20the%20YEAR%20function.%20For%20Example%2C%20%3CSTRONG%3E%3DYEAR(TODAY())%3C%2FSTRONG%3E%20will%20yield%202020%2C%20and%20you%20can%20subtract%20another%20field%2C%202012%2C%20from%20that%20to%20get%208.%3C%2FLI%3E%3C%2FOL%3E%3CP%3EAs%20I%20said%2C%20though%2C%20I%20didn't%20see%20in%20your%20spreadsheet%20any%20attempt%20at%20a%20formula%20incorporating%20a%20parameter%20such%20as%20you%20describe.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1813555%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20a%20Date%20Parameter%20in%20a%20Calculation%20substraction%20latest%20year%20from%202010%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1813555%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F425987%22%20target%3D%22_blank%22%3E%40mathetes%3C%2FA%3E%26nbsp%3B%20I%20added%20the%20Parameter%20and%20the%20created%20the%20calculated%20column%20and%20I%20want%20the%20calculated%20column%20to%20be%20fed%20from%20the%20parameter%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

I have a parameter that is a year.  Can it be passed to a calculated field?  The parameter will always grab the latest year and be subtracted from 2010.  So, if I get new data for 2020 the Parameter will grab the latest year and the calculation will use it for the subtraction (2019-2010).

3 Replies

@lmk001 

 

Absolutely it's possible. I don't see in your spreadsheet any spot where you're trying to do it, however. So it's hard to give anything specific. Let me just offer the following and invite you, if needed, to post a revised spreadsheet where you might include the formula where you're trying to make the calculation and not having it work.

First, though, let's see if a few pointers can help you make it work.

  1. Make sure that those numbers representing the years (the headings in some of your columns) are numbers and not text.  That appears to be the case.
  2. Then if you're using a full date (in Excel's "short date" format or some other), just enclose that in the YEAR function. For Example, =YEAR(TODAY()) will yield 2020, and you can subtract another field, 2012, from that to get 8.

As I said, though, I didn't see in your spreadsheet any attempt at a formula incorporating a parameter such as you describe.

@mathetes  I added the Parameter and the created the calculated column and I want the calculated column to be fed from the parameter

best response confirmed by lmk001 (Occasional Contributor)
Solution

@lmk001 

It's not necessary to keep parameter as a table, for only one value named cell is more suitable

image.png

It is taken as

// LatestYear
let
    Source = Text.From(
        Excel.CurrentWorkbook(){[Name="LatestYear"]}[Content][Column1]{0}
    )
in
    Source

Resulting table

image.png

is generated by

// Population with LatestYear
let
    Source = Excel.CurrentWorkbook(){[Name="CO_1_10"]}[Content],
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Renamed Columns" = Table.RenameColumns(#"Promoted Headers",{{"Column1", "Locality"}}),

    // New part
    #"Duplicated Column which has parameter as name" = Table.DuplicateColumn(
        #"Renamed Columns",
        LatestYear, "Difference"
    ),
    #"Replace Value in it with difference to 2010" = Table.ReplaceValue(
        #"Duplicated Column which has parameter as name",
        each [Difference],
        each [Difference] - [2010],
        Replacer.ReplaceValue,{"Difference"}
    )
in
    #"Replace Value in it with difference to 2010"

hope it is self-explainable.

 

If you always take the difference from the last column of the table, you may avoid using parameter at all:

let
//Population without Parameter

    Source = Excel.CurrentWorkbook(){[Name="CO_1_10"]}[Content],
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Renamed Columns" = Table.RenameColumns(#"Promoted Headers",{{"Column1", "Locality"}}),

    // Take last column name
    columnNames = Table.ColumnNames(#"Renamed Columns"),
    lastColumnName = List.Last(columnNames),
    // calculate difference
        #"Duplicated Last Column" = Table.DuplicateColumn(
        #"Renamed Columns",
        lastColumnName, "Difference"
    ),
    #"Replace Value in it with difference to 2010" = Table.ReplaceValue(
        #"Duplicated Last Column",
        each [Difference],
        each [Difference] - [2010],
        Replacer.ReplaceValue,{"Difference"}
    )
in
    #"Replace Value in it with difference to 2010"

In general second column [2010] also could be taken from headers names:

let
//Population without hardcoding

    Source = Excel.CurrentWorkbook(){[Name="CO_1_10"]}[Content],
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Renamed Columns" = Table.RenameColumns(#"Promoted Headers",{{"Column1", "Locality"}}),

    // Take last column name
    columnNames = Table.ColumnNames(#"Renamed Columns"),
    firstYearColumnName = columnNames{1},
    lastColumnName = List.Last(columnNames),

    // calculate difference
        #"Duplicated Last Column" = Table.DuplicateColumn(
        #"Renamed Columns",
        lastColumnName, "Last"
    ),
        #"Duplicated First Column" = Table.DuplicateColumn(
        #"Duplicated Last Column",
        firstYearColumnName, "First"
    ),
    #"Add Difference" = Table.AddColumn(
        #"Duplicated First Column",
        "Difference",
        each [Last] - [First]
    ),
    #"We don't need First and Last any more" = Table.RemoveColumns(
        #"Add Difference",
        {"Last", "First"}
    )
in
    #"We don't need First and Last any more"