you can query Azure Resource Graph to find which VMs are currently powered down. There is API/PowerShell/CLI support for Resource Graph.
| where type =~ 'microsoft.compute/virtualmachines' and properties.extended.instanceView.powerState.code has 'deallocated'
Please note that there is not yet VMSS support for power state. For VMSS, you should query the Azure Resource Manager API.
Having the list of deallocated VMs, you could then query Azure Activity Log for the last deallocate action and thus determine for how long your VM was deallocated.
An alternative to this, with a built-in recommendation that gives you VMs that have been deallocated for long, is the Azure Optimization Engine. Please note that you have to let it run at least 2 weeks to get accurate results.