I like that you are trying to build the procedure. @vknow360 also made a extension to calculate this.