I think that there are actually three different parts to your question, and maybe your confusion is coming from combining them all together.
The way I see it is in these parts:
- How to create a custom function.
- How to apply a function to a new column.
- How to apply a function to an existing column.
How to create a custom function
There are two main ways to create a custom function in Power Query:
Using the UI (follow steps here):
| Step |
Description |
Image |
| 1 |
Write your query |
 |
| 2 |
Parameterise your query |
 |
| 3 |
Create your function |
 |
Using only code (follow steps here):
- Example to filter a table:
let fun_FilterTable = (tbl_InputTable as table, txt_FilterValue as text) as table =>
let
Source = tbl_InputTable,
Filter = Table.SelectRows(DayCount, each Text.Contains([Column], txt_FilterValue))
in
Filter
in
fun_FilterTable
- Example to check if one string contains another:
let fun_CheckStringContains = (txt_String as text, txt_Check as text) as nullable logical =>
let
Source = txt_String,
Check = Text.Contains(Source, txt_Check)
in
Check
in
fun_CheckStringContains
More resources:
How to apply a function to a new column
Also has two different ways to achieve:
Custom Column (follow steps here):
| Step |
Description |
Image |
| 1 |
Create custom column |
 |
| 2 |
Add function |
 |
Custom Function (follow steps here):
| Step |
Description |
Image |
| 1 |
Invoke custom function |
 |
Sources:
How to apply a function to an existing column
Also has two different ways to achieve (unfortunately, only possible with pure code):
- Using Transformation:
- Example to uppercase an entire column:
let
Source = Table,
#"Uppercased text" = Table.TransformColumns(Source, {{"Column", each Text.Upper(_), type nullable text}})
in
#"Uppercased text"
- Example to add a prefix to all rows in one column:
let
Source = Table,
#"Added prefix" = Table.TransformColumns(Source, {{"Column", each "test_" & _, type text}})
in
#"Added prefix"
- Example to coerce column to date in Australian format:
let
Source = Table,
#"Fix date" = Table.TransformColumns(Source, {{"DateColumn", each Date.From(_, "en-AU"), type date}})
in
#"Fix date"
- Using Replacement
- Example to replace some text:
let
Source = Table,
#"Replaced value" = Table.ReplaceValue(Source, "Admin", "Administrator", Replacer.ReplaceText, {"Column"})
in
#"Replaced value"
- Example to replace with values from another column
let
Source = Table,
#"Replaced value" = Table.ReplaceValue(Source, each [FixThisColumn], each [OtherColumn], Replacer.ReplaceText, {"FixThisColumn"})
in
#"Replaced value"
Your Specific Problem
Without some dummy data to use, I have created some here, so that we can easily recreate the scenario from your example.
Data:
| ID |
ProgramStartDate |
ProgramEndDate |
| 1 |
1/Jan/2020 |
1/Dec/2021 |
| 2 |
1/Jan/2022 |
1/Mar/2023 |
| 3 |
1/Mar/2022 |
1/Dec/2022 |
| 4 |
1/Sep/2021 |
1/Dec/2023 |
| 5 |
1/Jan/2023 |
1/Dec/2023 |
I think that you should be using a combination of the PowerQuery in-build date functions (see here) and some of the PowerQuery conditional processes (see here).
My code would look something like this:
let
Source = Table.FromColumns({{1,2,3,4,5},{"1/Jan/2020","1/Jan/2022","1/Mar/2022","1/Sep/2021","1/Jan/2023"},{"1/Dec/2021","1/Mar/2023","1/Dec/2022","1/Dec/2023","1/Dec/2023"}},{"ID","ProgramStartDate","ProgramEndDate"}),
fix_Types = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"ProgramStartDate", type date}, {"ProgramEndDate", type date}}),
add_Today = Table.AddColumn(fix_Types, "DateToday", each Date.From(DateTime.LocalNow()), type date),
add_CheckCurrentYear = Table.AddColumn(add_Today, "IsInCurrentYear", each Date.IsInCurrentYear([DateToday]), type logical),
add_CheckProgramRunning = Table.AddColumn(add_CheckCurrentYear, "ProgramIsCurrent", each [DateToday]>[ProgramStartDate] and [DateToday]<[ProgramEndDate], type logical),
add_ConditionalCheck = Table.AddColumn(add_CheckProgramRunning, "DoSomething", each if [IsInCurrentYear] and [ProgramIsCurrent] then "Do Something" else null, type text)
in
add_ConditionalCheck
And the final output would look something like this:
| ID |
ProgramStartDate |
ProgramEndDate |
DateToday |
IsInCurrentYear |
ProgramIsCurrent |
DoSomething |
| 1 |
1/01/2020 |
1/12/2021 |
22/12/2022 |
TRUE |
FALSE |
null |
| 2 |
1/01/2022 |
1/03/2023 |
22/12/2022 |
TRUE |
TRUE |
Do Something |
| 3 |
1/03/2022 |
1/12/2022 |
22/12/2022 |
TRUE |
FALSE |
null |
| 4 |
1/09/2021 |
1/12/2023 |
22/12/2022 |
TRUE |
TRUE |
Do Something |
| 5 |
1/01/2023 |
1/12/2023 |
22/12/2022 |
TRUE |
FALSE |
null |