Search code examples
azureazure-logic-appsworkflow-definition-language

Azure logical app get first date of month


I have an Azure Logical app, I want to get the first date of the month two days ago.

For example: 
1. If today is '2022-08-01' then two days ago was '2022-07-29', I want to get '2022-07-01' as result
2. If today is '2022-08-02' then two days ago was '2022-07-31', I want to get '2022-07-01' as result
3. If today is '2022-08-03' then two days ago was '2022-08-01', I want to get '2022-08-01' as result

I already know I can get date for two days ago using "@addDays(utcNow(), -2, 'yyyy-MM-dd')" but I basically need first date of the month this date belongs to


Solution

  • You can use startOfMonth() function after calculating for 2 days ago date in order to achieve your requirement. Below is my Logic App flow.

    enter image description here

    RESULTS:

    enter image description here

    You can use the below code-view to reproduce the same in your environment.

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "Compose": {
                    "inputs": "@startOfMonth(outputs('Compose_2'),'yyyy-MM-dd')",
                    "runAfter": {
                        "Compose_2": [
                            "Succeeded"
                        ]
                    },
                    "type": "Compose"
                },
                "Compose_2": {
                    "inputs": "@addDays(variables('Date'), -2, 'yyyy-MM-dd')",
                    "runAfter": {
                        "Initialize_variable": [
                            "Succeeded"
                        ]
                    },
                    "type": "Compose"
                },
                "Initialize_variable": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "Date",
                                "type": "string",
                                "value": "2022-08-03"
                            }
                        ]
                    },
                    "runAfter": {},
                    "type": "InitializeVariable"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {},
            "triggers": {
                "manual": {
                    "inputs": {},
                    "kind": "Http",
                    "type": "Request"
                }
            }
        },
        "parameters": {}
    }